Jump to content

[SOLVED] random letters using round


redarrow

Recommended Posts

ok i got it wrong you use the rand statement but you still

dont get the exact 6 letters do you?

 

i am trying to get a six letter random letter for a test ok.

 

 

<?php

$x=range("a","z");

foreach($x as $d){

$rnd=$d{rand(0,6)};

echo $rnd;
}


?>

thorpe please brake it down the echoed code please cheers.

 

<?php

 $x = range('a','z'); //<< get that
 for ($i=0;$i<=6;$i++) { //<< get that
   echo $x[rand(0,25)]; // what is the squer bracket [] doing.
 }

?>

 

 

and this also works what is the brace doing cheers

 

<?php

  $x = range('a','z'); //<< get that
  for ($i=0;$i<=6;$i++) { //<< get that
    echo $x{rand(0,26)}; // what the brace {} doing.
  }

?>

// what all the [] for.

 

Because $x is an array. You retrieve array index's by using something like $x[4]. This would get the 5th element from the array $x (rememeber arrays start at 0, hance my change).

 

Within the [] we are running the rand() function to get a random index.

so why are we using a squere bracket []or a brace {} before the

varable what it for.

sorry but wont to learn properly.

 

ok a [] bracket wil open the inside of the array does that mean a {} also open the array.

 

 

// what all the [] for.

 

Because $x is an array. You retrieve array index's by using something like $x[4]. This would get the 5th element from the array $x (rememeber arrays start at 0, hance my change).

 

Within the [] we are running the rand() function to get a random index.

ok a [] bracket wil open the inside of the array does that mean a {} also open the array.

 

No. the {} syntax was the old method for indexing a string. eg;

 

<?php

  $s = "hello";
  echo $s{1}; // prints e

?>

 

However, I'm sure Ive read somewhere that this functionality is being depricated so I would avaoid it where possible. From memory, they may be switching string indexing to the square [] aswell. Id'e need to look it up.

From the manual.

 

Characters within strings may be accessed and modified by specifying the zero-based offset of the desired character after the string using square array-brackets like $str[42] so think of a string as an array of characters.

 

    Note: They may also be accessed using braces like $str{42} for the same purpose. However, using square array-brackets is preferred because the {braces} style is deprecated as of PHP 6.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.