Jump to content

Does anyone know how I should interpret this


fortnox007

Recommended Posts

Hi all, I just saw a small snippet on the internet and I wasn't sure on the { and } right after the variable.  In the snippet below they have a variable that uses it right behind it. So to make myself more clear I know what's going on inside the {..} but I have never seen those things right after a variable.

The closest thing I could think of is that it's a form of sub string. If anyone knows what it is and how to apply it on future cases I would love to hear it.  ::)

 

Here is a piece of the code (which is part of a while loop)

 

$string = $chars{mt_rand(0,strlen($chars))};

Hi all, I just saw a small snippet on the internet and I wasn't sure on the { and } right after the variable.  In the snippet below they have a variable that uses it right behind it. So to make myself more clear I know what's going on inside the {..} but I have never seen those things right after a variable.

The closest thing I could think of is that it's a form of sub string. If anyone knows what it is and how to apply it on future cases I would love to hear it.  ::)

 

Here is a piece of the code (which is part of a while loop)

 

$string = $chars{mt_rand(0,strlen($chars))};

Not a clue either, I've only seen brackets, maybe it's the same thing as

$string = $chars

{

mt_rand(0,strlen($chars));

}

???

:shrug: :shrug: :shrug:

You can access the characters of a string by using brackets like that, but it's depreciated as of PHP 6 in favor of the [] operator.

 

$chars = "abc";
echo $chars{2}; // c
// But you should do this:
echo $chars[2];

It should be written that way as of now. The [] operator will work for accessing characters of a string before PHP 6, and because it'll be the only non-depreciated way as of PHP 6 it's a good idea to only use [] now. Unless you want your code to break on a server running PHP 6.

hehe Just made a noobie function to echo all characters out on a new line. If someone can use it.. (i doubt that, but it's nice to fill up your screen)

 

$stringy = 'monkeys tend to eat banana\'s';
$length = strlen($stringy);

for ($i=0;$i<$length;$i++){
   echo $stringy[$i].'<br />';
}

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.