Re: ${0} variable basics

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 14-08-24 07:19 PM, Negin Nickparsa wrote:
why ${0} is valid but ${0 is not valid?
it means that curly braces evaluate what's inside them and that's why we
cannot have ${0?

variables shouldn't start from 0 so it means that it started from braces?
if yes why I cannot have ${0? and what is the variable here finally? $0 is
what it interprets? if yes so it is not a legal variable
I am totally mixed up.

The variable ${0} is a variable variable. For instance:

    $foo = 50;
    $fee = 'foo';
    echo ${$fee}."\n";   // This will output 50.
    echo ${'foo'}."\n";  // Again this will out 50.

To leave off the closing curly brace is to leave the expression unterminated. Similar to leaving off a parenthesis, square brace, or conditional brace. The engine needs to know the end of the variable to which it should retrieve it's value to then find the actual variable to look up in the current context. It is most like having an array and saying you want some indexed value from the array:

    $array[50]

You can't very well leave off the closing square brace or else the parser doesn't know where the index ends. In other words ${0} isn't a variable called "${0}" it's a variable referenced by the result of evaluating what lies between the curly braces. The following is perfectly valid:

    $foo1 = 1;
    $foo2 = 2;
    $foo3 = 3;

    for( $i = 1; $i <= 3; $i++ )
    {
        echo ${'foo'.$i};
        echo ${"foo$i"};   // Alternative.
    }

While you can't explicitly reference a variable called $0 you can set one into the context by using curly braces:

    ${0} = 100;
    echo ${0}."\n";

Since variables are denoted by the lead $ character I can't think of a good reason why a variable can't start with a number after the $ since the parser already knows it's working with a variable. I would imagine that it's a convention carried forward from C or other languages.

Cheers,
Rob.
--
Phone: 613-822-9060    +++    Cell: 613-600-2836
E-Mail Disclaimer: Information contained in this message and any
attached documents is considered confidential and legally protected.
This message is intended solely for the addressee(s). Disclosure,
copying, and distribution are prohibited unless authorized.

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php





[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux