Browsed by
Tag: PHP

[PHP] Variable with name of other variable – “variable variables”

[PHP] Variable with name of other variable – “variable variables”

When you write in PHP $$name, parser treats it as variable, wchich name is value of $name – it works like pointer: <? $var1 = ‘test1’; $var2 = ‘test2’; $a = ‘var1′; echo $$a.'<br>’; $a = ‘var2’; echo $$a; ?><? $var1 = ‘test1’; $var2 = ‘test2’; $a = ‘var1′; echo $$a.'<br>’; $a = ‘var2’; echo $$a; ?> output: test1 test2test1 test2

[PHP] Execution/evaluation of code from variable

[PHP] Execution/evaluation of code from variable

In PHP we can use function eval in order to evaluate given variable (string) as PHP code: <? $var = ‘for ($i=0;$i<5;$i++) { echo $i.\'<br>\’; }’; eval($var); ?><? $var = ‘for ($i=0;$i<5;$i++) { echo $i.\'<br>\’; }’; eval($var); ?> output: 0 1 2 3 40 1 2 3 4