K
K
knekrasov2011-12-01 23:02:11
PHP
knekrasov, 2011-12-01 23:02:11

Is $ operator in php?

I'm developing a PHP parser and I've come across such an ambiguous question.
As you know, the official language specification simply does not exist. At the office website, there is no $ sign in the operations priority table .
Nevertheless, expressions of the form

<?php
function testMe($a) {
  //do something
}

$func = 'testMe';

$func(21);

Moreover, the following example will print 15 (php 5.3.8)
<?php

$myVar = 14;
$t = 'myVar';
$$t += 1;
echo $myVar;

What is your opinion, can $ be considered an operator?

Answer the question

In order to leave comments, you need to log in

9 answer(s)
G
gaelpa, 2011-12-02
@gaelpa

Have you tried to look in the sources of the Zend PHP parser?
Maybe there is a more accurate answer.

E
edogs, 2011-12-01
@edogs

Not an operator.
As for the syntax, it is sometimes curious to look into ru2.php.net/manual/en/tokens.php

B
Bodigrim, 2011-12-02
@Bodigrim

It seems that not only constructs like $$var are valid, but also more complex ones. For example, the following code outputs 1111.

<?php

$a=1;
echo $a;

$b="a";
echo $$b;

$c="b";
echo $$$c;

$d="c";
echo $$$$d;

?>

R
rPman, 2011-12-01
@rPman

no, because then the following constructs will be valid:
$($a)

M
MT, 2011-12-01
@MTonly

en.php.net/manual/en/language.variables.basics.php
en.php.net/manual/en/language.variables.variable.php

A
Anatoly, 2011-12-01
@taliban

This is not an operator, this is the “beginning” of a variable, and also constructions like $$ mean (roughly speaking) a variable in the variable name and were provided by the creator of the language initially, just like {} - it tritely selects a variable from a string, although it works without strings .

V
Vladimir Chernyshev, 2011-12-01
@VolCh

Although it is close in function to the pointer dereference operator, it cannot be considered an operator. Just an element of variable name syntax.

@
@ngreduce, 2011-12-01
_

The operator does something.
For example echo outputs, require includes.
$ and $$ simply show the interpreter how to deal with this or that variable.
BUT
www.php.net/manual/en/language.operators.php
The variable symbol '$' should be considered as the highest-precedence operator, so that the variable variables such as $$a[0] won't confuse the parser.

S
Sergey Beresnev, 2011-12-02
@sectus

Can be treated as the highest precedence operator.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question