Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Variable without name: ${''}

Tags:

php

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

like image 416
Akerus Avatar asked Jun 19 '26 22:06

Akerus


1 Answers

${''} is a valid variable which name happens to be an empty string. 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
like image 172
invisal Avatar answered Jun 22 '26 12:06

invisal