Php-mysql Course

A function that returns the lower, higher, and closest number. The function receives two parameters: an array with numbers, and the number. Compares that number with the numbers from array, than returns an associative array with three elements: the "lower", "higher", and "closest" number.
<?php
// retutrs an array with the number from $array: lower, higher and closest to $nr
function closestLowerHigherNr($array, $nr) {
  // PHP-MySQL Course - https://coursesweb.net/php-mysql/
  sort($array);      // Sorts the array from lowest to highest

  // sets the array that will be returned, initially with the lowest and highest number from $array
  $re_arr = array('lower'=>min(current($array), $nr), 'higher'=>max(end($array), $nr), 'closest'=>$nr);

  // traverse the numbers, stores in $re_arr the number immediately lower and higher than $nr
  foreach($array AS $num){
    if($nr > $num) $re_arr['lower'] = $num;
    else if($nr <= $num){
      // if the current number from $array is equal to $nr, or immediately higher, stores that number
      // and stops the foreach() loop
      $re_arr['higher'] = $num;
      break;
    }
  }

  // here it gets the closest number to $nr
  // (the number ('lower' or 'higher') with the lowest diferrence between its value and $nr)
  $re_arr['closest'] = (abs($nr - $re_arr['lower']) < abs($re_arr['higher'] - $nr)) ? $re_arr['lower'] : $re_arr['higher'];

  return $re_arr;
}

// Example
$numbers = array(-8, -3, 0, 5.8, 12, 9, 2.1);
$test1 = closestLowerHigherNr($numbers, -6);
$test2 = closestLowerHigherNr($numbers, 3);
$test3 = closestLowerHigherNr($numbers, 9);

// see the values in $test1, $test2, $test3
var_export($test1);       // array ( 'lower' => -8, 'higher' => -3, 'closest' => -8 )
var_export($test2);       // array ( 'lower' => 2.1, 'higher' => 5.8, 'closest' => 2.1 )
var_export($test3);       // array ( 'lower' => 5.8, 'higher' => 9, 'closest' => 9 )
?>
This function works also if $nr is lower, or higher than any number in $array.
Example:
<?php
// here add the closestLowerHigherNr() function

$numbers = array(-8, 2, 12);
$test4 = closestLowerHigherNr($numbers, -23);      // -23 is lower than any number in $numbers
$test5 = closestLowerHigherNr($numbers, 18);       // 18 is higher than any number in $numbers

// see the values in $test4, $test5
var_export($test4);       // array ( 'lower' => -23, 'higher' => -8, 'closest' => -23 )
var_export($test5);       // array ( 'lower' => 12, 'higher' => 18, 'closest' => 18, )
?>

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
Which tag is a block element?
<div> <img> <span>
<div>Web Programming and Development</div>
Which CSS code displays the text underlined?
font-style: italic; text-decoration: underline; font-weight: 500;
h2 {
  text-decoration: underline;
}
Click on the JavaScript function that can access other function after a specified time.
insertBefore() setTimeout() querySelector()
function someFunction() { alert("CoursesWeb.net"); }
setTimeout("someFunction()", 2000);
Click on the instruction that returns the number of items of a multidimensional array in PHP.
count($array) count($array, 1) strlen()
$food =["fruits" =>["banana", "apple"), "veggie" =>["collard", "pea"));
$nr_food = count($food, 1);
echo $nr_food;       // 6
Get Lower, Higher, and Closest Number

Last accessed pages

  1. Zodiac Signs JavaScript code (11028)
  2. The School for Gods (5797)
  3. The Prayer of the Frog (2053)
  4. PhpSpreadsheet - Read, Write Excel and LibreOffice Calc files (26015)
  5. Importing images (1577)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (331)
  2. Read Excel file data in PHP - PhpExcelReader (124)
  3. The Four Agreements (97)
  4. PHP Unzipper - Extract Zip, Rar Archives (94)
  5. The Mastery of Love (89)
Chat
Chat or leave a message for the other users
Full screenInchide