Akerus Akerus - 1 year ago 95
PHP Question

Variable without name: ${''}

So variable variables are existing. Meaning that this is working

$a = 'test';
$$a = 'Hello';
echo ${'test'}; //outputs 'Hello'


But now I've come across some rather strange code using a variable without a name:

function test(&$numRows) {
$numRows = 5;
echo ' -- done test';
}

$value = 0;
test($value);
echo ' -- result is '.$value;

test(${''}); //variable without name


http://ideone.com/gTvayV Code fiddle

Output of this is:


-- done test -- result is 5 -- done test


That means, the code is not crashing.

Now my question is: what exactly happens if
$numRows
value is changed when the parameter is a variable without name? Will the value be written into nirvana? Is that the PHP variable equivalent to
/dev/null
?
I wasn't able to find anything specific about this.

Thanks in advance

Answer Source

If you have never set it before, it is undefined.

var_dump(isset(${''}));   // if you have never set it before, it is undefined.

You don't see any error because you disabled the NOTICE error message.

error_reporting(E_ALL);
ini_set('display_errors', 1);

echo ${''}; // Notice: Undefined variable:

You can set it like this:

${''} = 10;
echo ${''};  // shows 10
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download