Jump to content

[SOLVED] random letters using round


redarrow

Recommended Posts

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;
}

?>

Link to comment
Share on other sites

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;
}


?>

Link to comment
Share on other sites

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.
  }

?>

Link to comment
Share on other sites

// 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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.