redarrow Posted June 5, 2007 Share Posted June 5, 2007 why dosent this work all i get is the letter a. <?php $x=range("a","z"); $result=$x{round(0,6)}; echo $result; ?> this wont work as well. <?php $x=range("a","z"); foreach($x as $d){ $rnd=$d{round(0,6)}; echo $rnd; } ?> Quote Link to comment Share on other sites More sharing options...
trq Posted June 5, 2007 Share Posted June 5, 2007 why dosent this work all i get is the letter a. What exactly do you expect to happen? The code makes no sense. Are you sure your not looking for rand? Quote Link to comment Share on other sites More sharing options...
redarrow Posted June 5, 2007 Author Share Posted June 5, 2007 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; } ?> Quote Link to comment Share on other sites More sharing options...
trq Posted June 5, 2007 Share Posted June 5, 2007 If you want 6 random letters. <?php $x = range('a','z'); for ($i=0;$i<=6;$i++) { echo $x[rand(0,26)]; } ?> Quote Link to comment Share on other sites More sharing options...
trq Posted June 5, 2007 Share Posted June 5, 2007 Sorry... should be. echo $x[rand(0,25)]; Quote Link to comment Share on other sites More sharing options...
redarrow Posted June 5, 2007 Author Share Posted June 5, 2007 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. } ?> Quote Link to comment Share on other sites More sharing options...
trq Posted June 5, 2007 Share Posted June 5, 2007 // 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. Quote Link to comment Share on other sites More sharing options...
redarrow Posted June 5, 2007 Author Share Posted June 5, 2007 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. Quote Link to comment Share on other sites More sharing options...
trq Posted June 5, 2007 Share Posted June 5, 2007 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. Quote Link to comment Share on other sites More sharing options...
trq Posted June 5, 2007 Share Posted June 5, 2007 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. Quote Link to comment Share on other sites More sharing options...
redarrow Posted June 5, 2007 Author Share Posted June 5, 2007 Thank u for ur grate support thorpe i am studying your code cheers m8. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.