Learn Codeigniter

Create Random String In PHP

We will use following PHP code snippet to create Random String In PHP .

Example

<?php
function randomString($length) {
  $characters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
  $randomString = '';
  $size = strlen( $characters );
  for( $i = 0; $i < $length; $i++ ) {
    $randomString .= $characters[ rand( 0, $size - 1 ) ];
  }
  return $randomString;
}
echo randomString(7);
?>

Output

0wwe5HL