Edupala

Comprehensive Full Stack Development Tutorial: Learn Ionic, Angular, React, React Native, and Node.js with JavaScript

Custom Php Function

Php function to generate a unique random number between two range and creates an array with  keys and another for its values.

function uniqueNumber($min, $max, $quantity) {
   $numbers = range($min, $max);
   shuffle($numbers);
   return array_slice($numbers, 0, $quantity);
}

$pageno = array(1,2,3,4,5);
$randomPageno = uniqueNumber(1,5,5);
$mapPage = array_combine($pageno, $randomPageno);
print_r ($mapPage);

Output : Array ( [1] => 5 [2] => 1 [3] => 3 [4] => 2 [5] => 4 )

Where array() will generate new array with initial value pass as parameter,

uniqueNumber(1,5,5) – custom function to generate an array with a unique number between two range.

2. Php function to round up the rating to star rating if rating decimal value between .75 to .99 then add one to star rating, and if the decimal value is between .25 to .74 then we have to make rating to .5 and if rating decimal is less then .25 then we have to set decimal to zero.

function rate_round($rating){
    $whole = floor($rating);
    $fraction = $rating – $whole;

    if($fraction >= .75 && $fraction <= .99){ $ratingStar = floor($rating) +1; } elseif ($fraction >= .25     && $fraction <= .74){
       $fraction = .5;
      $ratingStar = floor($rating) + $fraction;
   } else{
       $ratingStar = floor($rating);
   }
   return $ratingStar;
}),
Custom Php Function

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top