Posted By

daGrevis on 02/24/11


Tagged

random


Versions (?)

random_value()


 / Published in: PHP
 

This function generates random value from given parameters (length and characters).

  1. function random_value( $length = 6, $characters = array( 'a', 'b', 'c' ) ) { // Parameters are optional.
  2.  
  3. $data = ''; // Define empty string for later (return $data).
  4.  
  5.  
  6. $from_zero = count( $characters ) - 1; // Highest possible index of array $characters.
  7.  
  8. for( $i = 0; $i < $length; $i++ ) { // Loop will execute the same times as value of variable $lenght.
  9.  
  10. $data .= $characters[ mt_rand( 0, $from_zero ) ]; // Adds one random symbol from array $characters each time loop executes.
  11.  
  12. }
  13.  
  14.  
  15. return $data;
  16.  
  17. } // By daGrevis.
  18.  
  19.  
  20. ##
  21.  
  22.  
  23. echo random_value( 12, array( 'x', 'y', 0 ) ); // Usage with given parameters.
  24. echo '<br />'; // Line break. xD
  25. echo random_value(); // Usage without parameters (default would apply (see in function)).

Report this snippet  

You need to login to post a comment.