Php-mysql Course

PHP function that returns an Array with Unique Random Numbers or Letters. The function receives three arguments: the number of items in array ($nri), the lowest number or letter ($min), and the highest number or letter ($max).
/*
 Function to create an array of unique random numbers or letters
 Receives 3 arguments:
   $nri = number of items in array
   $min = the lowest number, or letter
   $max = the highest number, or letter
*/
function randomNrLtArray($nri, $min, $max) {
  // From: https://coursesweb.net/php-mysql/
  // creates an array with values from $min to $max
  $arr = range($min, $max);

  // randomizes the order of the elements in $arr, extracts and keeps the first $nri items
  shuffle($arr);
  $arr = array_slice($arr, 0, $nri);

  return $arr;      // returnns the array
}
- Example: gets an array with 5 random numbers from 1 to 50, and another array with 4 random letters from "A" to "M".
<?php
/*
 Function to create an array of unique random numbers or letters
 Receives 3 arguments:
   $nri = number of items in array
   $min = the lowest number, or letter
   $max = the highest number, or letter
*/
function randomNrLtArray($nri, $min, $max) {
  // From: https://coursesweb.net/php-mysql/
  // creates an array with values from $min to $max
  $arr = range($min, $max);

  // randomizes the order of the elements in $arr, extracts and keeps the first $nri items
  shuffle($arr);
  $arr = array_slice($arr, 0, $nri);

  return $arr;      // returnns the array
}

// array with 5 random numbers from 1 to 50
$nrs = randomNrLtArray(5, 1, 50);

// array with 4 random letters from "A" to "M"
$lts = randomNrLtArray(4, 'A', 'M');

// Test
var_export($nrs);
echo '<br/>';
var_export($lts);
?>
Results:
array (0 => 6, 1 => 23, 2 => 42, 3 => 47, 4 => 8)
array (0 => 'J', 1 => 'D', 2 => 'F', 3 => 'G')

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
Which tag is used in <table> to create table header cell?
<thead> <th> <td>
<table><tr>
  <th>Title 1</th>
  <th>Title 2</th>
</tr></table>
Which CSS property sets the distance between lines?
line-height word-spacing margin
.some_class {
  line-height: 150%;
}
Which function opens a new browser window.
alert() confirm() open()
document.getElementById("id_button").onclick = function(){
  window.open("http://coursesweb.net/");
}
Indicate the PHP function that returns an array with names of the files and folders inside a directory.
mkdir() scandir() readdir()
$ar_dir = scandir("dir_name");
var_export($ar_dir);
Create Array of Unique Random Numbers or Letters

Last accessed pages

  1. Rectangle, Oval, Polygon - Star (3323)
  2. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (141764)
  3. Download PHP-MySQL resources (1156)
  4. AJAX and XML (2351)
  5. PHP PDO - Introduction and Connecting to Databases (8631)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (488)
  2. CSS cursor property - Custom Cursors (81)
  3. The Mastery of Love (73)
  4. PHP-MySQL free course, online tutorials PHP MySQL code (64)
  5. CSS3 2D transforms (46)