Php-mysql Course

To get a random number in PHP, you can use the mt_rand() function (there is also rand() function, but, according to PHP documentation, mt_rand() is faster).
- Example:
$nr = mt_rand();
echo $nr;

• To get a random number between a minimum and maximum number, use this syntax:   mt_rand($min, $max).
$nr = mt_rand(1000, 9999);
echo $nr;

Random numbers from array

If you have an array of numbers, and you want to get an random number from that array, you can use the array_rand() function. This function returns the key for a random entry.
- Example:
$nrs = array(123, 567, 4556, 34245, 78);
$rk = array_rand($nrs);
$nr = $nrs[$rk];
echo $nr;       // 4556

• If you want to pick more than one random key from array, add a seccond argument that specifies the number of keys (less than the number of elements of that array). In this case, array_rand($array, $nrk) will return an array with $nrs number of random keys from $array.
$nrs = array(123, 567, 4556, 34425, 78, 789);
$rks = array_rand($nrs, 2);     // contains 2 random keys (2, 5)

echo $nrs[$rks[0]];      // 4556
echo $nrs[$rks[1]];      // 789

Random number with distinct digits

If you want to get a random number between a minimum and maximum number, but with distinct digits, without repeated character in the random number, you can use the following function.
<?php
// returns a random number between $min and $max, with distinct digits
function getDistinctNr($min, $max) {
 // random number with distinct digits ( https://coursesweb.net )
  $nrstr = (string) mt_rand($min, $max);     // get random number, converted into string
  $n_nr = strlen($nrstr);           // number of characters
  $setnr = array();           // to store distinct digits that will form the returned number

  // traverse the characters of $nrstr to add in $setnr only its distinct digits
  // if number already in $setnr, traverse 0 to 9 to define another distinct number
  for($i=0; $i<$n_nr; $i++) {
    if(!in_array($nrstr[$i], $setnr)) $setnr[] = $nrstr[$i];
    else {
      for($i2=0; $i2<10; $i2++) {
        if(!in_array($i2, $setnr)) {
          $setnr[] = $i2;
          break;
        }
      }
    }
  }

  return implode('', $setnr);
}

// Test
$nr = getDistinctNr(1000, 9999);
echo $nr;
?>

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
Which tag create a highlighted bolded text?
<q> <strong> <em>
<p>Address: <strong>http://CoursesWeb.net/</strong> - Tutorials.</p>
Which of these CSS codes displays the text bolded?
text-size: 18px; font-style: italic; font-weight: 800;
#id {
  font-weight: 800;
}
What JavaScript function can be used to call another function multiple times, to a specified time interval?
setInterval() setTimeout() push()
function someFunction() { alert("CoursesWeb.net"); }
setInterval("someFunction()", 2000);
Click on the correctly defined variable in PHP.
var vname = 8; $vname = 8; $vname == 8;
$vname = 8;
echo $vname;
Random numbers in PHP

Last accessed pages

  1. The School for Gods (5790)
  2. PhpSpreadsheet - Read, Write Excel and LibreOffice Calc files (26010)
  3. Display UL bullets and OL numbers on the right side (8080)
  4. $_GET, $_POST and $_REQUEST Variables (33720)
  5. Animating CSS properties with jQuery (1153)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (308)
  2. Read Excel file data in PHP - PhpExcelReader (108)
  3. The Four Agreements (93)
  4. PHP Unzipper - Extract Zip, Rar Archives (90)
  5. The Mastery of Love (84)