Variables (symbolic links)

Lecture



Sometimes it is convenient to have variable variable names. That is, the name of a variable that can be determined and changed dynamically. A normal variable is defined like this:

<?php
$a = "hello";
?>

A variable takes the value of a variable and treats it as the name of a variable. In the above example, hello can be used as a variable name with two dollar signs. I.e:

<?php
$$a = "world";
?>

Now, the PHP symbol tree contains two variables: $ a , containing "hello", and $ hello , containing "world". Thus, the following expression:

<?php
echo "$a ${$a}";
?>

will output the same as the following expression:

<?php
echo "$a $hello";
?>

that is, they both bring out: hello world .

In order to use variable variables with arrays, you must solve the problem of ambiguity. That is, if you write $$ a [1] , the handler needs to know whether you want to use $ a [1] as a variable, or you need it as a variable $$ a , and then its index [1]. The syntax for resolving this ambiguity is as follows: $ {$ a [1]} for the first case and $ {$ a} [1] for the second.

Attention! Note that variables cannot be used with predefined PHP variables. This means that you cannot do something like $ {$ _ GET} . If you are looking for a way to use superglobal variables and old HTTP _ * _ VARS , you can try to reference them.

Variables are also called symbolic links.


Comments


To leave a comment
If you have any suggestion, idea, thanks or comment, feel free to write. We really value feedback and are glad to hear your opinion.
To reply

Running server side scripts using PHP as an example (LAMP)

Terms: Running server side scripts using PHP as an example (LAMP)