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

Link to comment
Share on other sites

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:

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

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.