Discussion:
${0} variable basics
Negin Nickparsa
2014-08-24 23:19:53 UTC
Permalink
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.


Sincerely
Negin Nickparsa
Robert Cummings
2014-08-25 03:45:42 UTC
Permalink
Post by Negin Nickparsa
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
Domain nikha.org
2014-08-25 10:04:03 UTC
Permalink
Post by Negin Nickparsa
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.
Sincerely
Negin Nickparsa
Hi, I suppose, that ${0} is interpreted as type "string". And that's legal.
And ${0 is parsed as an unbalanced curly brace. And that's illegal...

May be ;-)
Cheers Niklaus
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Christoph Becker
2014-08-25 14:25:50 UTC
Permalink
Post by Negin Nickparsa
why ${0} is valid but ${0 is not valid?
it means that curly braces evaluate what's inside them
No, at least not in the general case. This notation is called "simple
syntax" in the PHP manual[1] and "Variable-Name Creation
Operator/Expression" in the current draft of the PHP language
specification[2].
Post by Negin Nickparsa
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?
Yes, indeed. However, the notation $0 would be a syntax error.
Post by Negin Nickparsa
if yes so it is not a legal variable
Well, actually it is a legal variable, but it might better be avoided to
use such variables. They are confusing, and I can't think of any
advantage in using such variables. Use $zero instead, or maybe an array.
Post by Negin Nickparsa
I am totally mixed up.
[1]
<http://php.net/manual/en/language.types.string.php#language.types.string.parsing.simple>
[2]
<https://github.com/php/php-langspec/blob/master/spec/10-expressions.md#user-content-variable-name-creation-operator>
--
Christoph M. Becker
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Negin Nickparsa
2014-08-25 22:09:41 UTC
Permalink
Sincerely
Negin Nickparsa
Post by Christoph Becker
Post by Negin Nickparsa
why ${0} is valid but ${0 is not valid?
it means that curly braces evaluate what's inside them
No, at least not in the general case. This notation is called "simple
syntax" in the PHP manual[1] and "Variable-Name Creation
Operator/Expression" in the current draft of the PHP language
specification[2].
Post by Negin Nickparsa
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
Post by Negin Nickparsa
what it interprets?
Yes, indeed. However, the notation $0 would be a syntax error.
Post by Negin Nickparsa
if yes so it is not a legal variable
Well, actually it is a legal variable, but it might better be avoided to
use such variables. They are confusing, and I can't think of any
advantage in using such variables. Use $zero instead, or maybe an array.
​yes Christoph! all I was saying that I cannot understand it evaluates to
0​ but it is legal to write it that way but it is not legal before
interpretation.Thank you
Post by Christoph Becker
Post by Negin Nickparsa
I am totally mixed up.
[1]
<
http://php.net/manual/en/language.types.string.php#language.types.string.parsing.simple
[2]
<
https://github.com/php/php-langspec/blob/master/spec/10-expressions.md#user-content-variable-name-creation-operator
--
Christoph M. Becker
Domain nikha.org
2014-08-25 19:51:33 UTC
Permalink
Post by Christoph Becker
Post by Negin Nickparsa
why ${0} is valid but ${0 is not valid?
it means that curly braces evaluate what's inside them
No, at least not in the general case. This notation is called "simple
syntax" in the PHP manual[1] and "Variable-Name Creation
Operator/Expression" in the current draft of the PHP language
specification[2].
Post by Negin Nickparsa
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?
Yes, indeed. However, the notation $0 would be a syntax error.
Post by Negin Nickparsa
if yes so it is not a legal variable
Well, actually it is a legal variable, but it might better be avoided to
use such variables. They are confusing, and I can't think of any
advantage in using such variables. Use $zero instead, or maybe an array.
Post by Negin Nickparsa
I am totally mixed up.
[1]
<http://php.net/manual/en/language.types.string.php#language.types.string.parsing.simple>
[2]
<https://github.com/php/php-langspec/blob/master/spec/10-expressions.md#user-content-variable-name-creation-operator>
--
Christoph M. Becker
The manual is not realy clear in this point. My practical expierence says this:
an expression like "${....}" is always evaluated as string. ${0} creates a variable named "0", because "0" is not the integer, that would be illegal.

And BTW: ${0000.578} gives you "0000_578", because dots in variable names are not allowed and automatically converted to underscores. And it is NOT evaluated to a float.

Confusing, and indeed avoid it! Why the hell you really need a variable named "0"?
The fabulous type juggling of PHP operates on the values, not the names of variables!
--
Niklaus
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Negin Nickparsa
2014-08-25 22:06:53 UTC
Permalink
Sincerely
Negin Nickparsa
Post by Negin Nickparsa
Post by Christoph Becker
Post by Negin Nickparsa
why ${0} is valid but ${0 is not valid?
it means that curly braces evaluate what's inside them
No, at least not in the general case. This notation is called "simple
syntax" in the PHP manual[1] and "Variable-Name Creation
Operator/Expression" in the current draft of the PHP language
specification[2].
Post by Negin Nickparsa
and that's why we
cannot have ${0?
variables shouldn't start from 0 so it means that it started from
braces?
Post by Christoph Becker
Post by Negin Nickparsa
if yes why I cannot have ${0? and what is the variable here finally?
$0 is
Post by Christoph Becker
Post by Negin Nickparsa
what it interprets?
Yes, indeed. However, the notation $0 would be a syntax error.
Post by Negin Nickparsa
if yes so it is not a legal variable
Well, actually it is a legal variable, but it might better be avoided to
use such variables. They are confusing, and I can't think of any
advantage in using such variables. Use $zero instead, or maybe an array.
Post by Negin Nickparsa
I am totally mixed up.
[1]
<
http://php.net/manual/en/language.types.string.php#language.types.string.parsing.simple
Post by Christoph Becker
[2]
<
https://github.com/php/php-langspec/blob/master/spec/10-expressions.md#user-content-variable-name-creation-operator
Post by Christoph Becker
--
Christoph M. Becker
an expression like "${....}" is always evaluated as string. ${0} creates a
variable named "0", because "0" is not the integer, that would be illegal.
And BTW: ${0000.578} gives you "0000_578", because dots in variable names
are not allowed and automatically converted to underscores. And it is NOT
evaluated to a float.
Confusing, and indeed avoid it!
​
The fabulous type juggling of PHP operates on the values, not the names of variables!
​
​
Why the hell you really need a variable named "0"?​

​:) Niklaus ​

Actually I guess nobody need that and you're absolutely right.
It was a practice question that I failed to answer correctly and I was
frustrated too.

Thank you so much for your guidance.


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.

Robert, Thank you so much for nice explanation and examples I really
appreciated.
--
Post by Negin Nickparsa
Niklaus
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Loading...