I was expecting to see a notice like PHP Notice: Undefined variable: a, however when I run this code it works perfectly fine. Moreover, the variable becomes NULL. Why?
<?php
function($a = 1) use (&$a) {};
var_dump($a); // NULL <---- Expected a Notice "undefined", got "NULL"
Demo
i think the & operator silently create the variable if it doesn't exist already. seems this page try to explain it:
If you assign, pass, or return an undefined variable by reference, it will get created. , so when you do use (&$a) , & sees that $a doesn't already exist, and create it..
as for undefined, that's not a real type in PHP. (unlike, for example, javascript, which actually have a type called undefined, and a separate type called null)
why null? given that undefined doesn't exist, its the only logical choice for the initial value, what did you expect it to be initialized to?
"If you don't initialize your variables with a default value, the PHP will do a type cast depending on how you are using the variable. This sometimes will lead to unexpected behaviour."
Related
I have tried the following code:
<?php
echo gettype($x);
?>
And I got the following output:
Why did gettype() outputted "NULL" after the error was displayed? I mean an undefined variable is a variable that doesn't exist and not a NULL variable, right?
The documentation of NULL explains:
The special NULL value represents a variable with no value. NULL is the only possible value of type null.
A variable is considered to be null if:
it has been assigned the constant NULL.
it has not been set to any value yet.
it has been unset().
Also check the table "Comparisons of $x with PHP functions".
You kinda did answer your own question.
At the moment you're trying to get the type of nothing it will be null by default.
For example
X = 5
It will return a integer
But in your case x wasn't initiated and therefore it stays null.
Vars work on the run and can be anything.
I hope this is somehow useful.
(if this contains a misconception please let me know what is wrong)
My assumption followed by question based on the assumption:
Javascript has null and undefined. You can set a variable to be null, implying it has no value, or you can set it to undefined, implying that it isn't known whether it has a value or not - it's just not set at all.
PHP has null, which so far I've used in the same way I'd use null in Javascript. Does PHP have an equivalent of undefined?
Not really, undefined has no counterpart in PHP. Comparing JS's undefined to PHP
s null doesn't really stack up, but it's as close as you're going to get. In both languages you can assign these values/non-values:
var nothingness = undefined;
$nothing = null;
and, if you declare a variable, but don't assign it anything, it would appear that JS resolves the var to undefined, and PHP resolves (or assigns) that variable null:
var nothingness;
console.log(nothingness === undefined);//true
$foo;
echo is_null($foo) ? 'is null' : '?';//is null
Be careful with isset, though:
$foo = null;
//clearly, foo is set:
if (isset($foo))
{//will never echo
echo 'foo is set';
}
The only real way to know if the variable exists, AFAIK, is by using get_defined_vars:
$foo;
$bar = null;
$defined = get_defined_vars();
if (array_key_exists($defined, 'foo'))
{
echo '$foo was defined, and assigned: '.$foo;
}
I don't think there is undefined. Rather than checking if something is undefined as you would in JavaScript:
if(object.property === undefined) // Doesn't exist.
You just use isset():
if(!isset($something)) // Doesn't exist.
Also, I think your understanding of undefined is a little odd. You shouldn't be setting properties to undefined, that's illogical. You just want to check if something is undefined, which means you haven't defined it anywhere within scope. isset() follows this concept.
That said, you can use unset() which I guess would be considered the same as setting something to undefined.
No. It only has "unset", which is not actually a value at all.
undefined is the value you'll get in Javascript when trying to access a property/variable which does not exist. You don't typically set something to undefined.
PHP does not have this mechanism. If you're trying to access a property or variable which does not exist, you will trigger an error of the level E_NOTICE. The script will continue, but the value of the non-existent variable will be substituted by null.
In Javascript you try to access a variable and test whether it you get undefined or not.
In PHP, you use isset($var) to test whether a variable exists before accessing it.
There are some scenarios where the null is a valid value, and there needs to be some other data type to signify the result of an action.
For example, I have a function to get the value of some key in a complex/nested array. The key value can be anything (including null). What should I return in this case if the key path is not valid? Returning null, false, -1, etc. wouldn't make any sense because the key value might be one of these.
What I decided to is define a constant:
define("undefined", "__undefined__");
...
$value = getKeyValue($options, ["section", "subsection", "opt2"]);
if ($value != undefined) {
...
}
Just playing around and I found this.
Why the call by reference to $this->newAxis() does not throw an undefined property notice (xAxis property), while the var_dump() does?
public function newXAxis()
{
// var_dump(isset($this->xAxis)); // false
// var_dump($this->xAxis); // Throws the notice
return $this->newAxis($this->xAxis); // Should throw the notice?!
}
protected function newAxis(&$current)
{
// ...
}
Does it have something to do with the passing by reference, thus not accessing the property directly?
Yes, it happens because you pass it by reference. When you pass by value, an attempt is made to actually read the value of the variable - so a notice appears. When you pass by reference, the value does not need to be read.
When you do that, the variable/property is created if it does not exist yet.
From the manual:
If you assign, pass, or return an undefined variable by reference, it
will get created.
<?php
function foo(&$var) { }
foo($a); // $a is "created" and assigned to null
newAxis(&$current)
is pass by reference. that means you are passing a variable.
By default all variables in PHP are undefined.
You define them by just using, e.g.
$a = 1;
As you can see, PHP does not complain here that $a is undefined, right?
Okay ;), see here:
$a = $b;
PHP now complains that $b is undefined.
Like with $a (you define the variable) and $b (the variable is not defined) it is with passing by reference or by value:
$this->newAxis($a);
The variable $a is defined when passed by reference. It carries it's default value NULL. And now the $b example:
var_dump($b);
var_dump takes it's parameters by value. Therefore PHP complains that $b is not defined.
And that's all. I hope it was clear enough.
I'm going on a limb here...
Since you are accessing it as an object (from a class) it won't give you a notice, while when you var_dump something it kind of access it like an array (and since it's empty it throws a notice)
Last time I'm exploring PHP pretty much and I was curious if it's possible to define variable without initializing it like in C++.
Well interpreter doesn't output an fatal eror (only a notice that variable test is undefined) if I'll run this code:
<?php
$test = (int) $test;
?>
And if I try to check it with var_dump() function i get:
int(0)
I assumed interpreter automatically cast undefined to integer. Well, ok it's pretty clever.
But when I removed code repsonsible for type casting and checked it with var_dump() function I get:
NULL
Well, ok. So when I assign undefined variable as undefined variable I get variable with NULL. I can understand interpreter do it for me on the run. But when I try something like this:
<?php
var_dump($test);
var_dump($test);
?>
I get two notices that test is not defined, but var_dump() returns NULL, not undefined. And now I don't get it. If I'll turn off notices var_dump() function will have same result with undefined variables and variables assigned to NULL. And here comes a question from topic. Why interpreter (or rather a var_dump() function) treats undefined and NULL as same ?
The special NULL value represents a variable with no value. NULL is the only possible value of type NULL.
A variable is considered to be null if:
it has been assigned the constant NULL.
it has not been set to any value yet.
it has been unset().
(int)$test = casting, force a value to data type (integer)
warning notices = cause by $test is never defined, and you trying to use it
var_dump($test) = I dun have a value for $test, so, I return you a null (by PHP)
I want to use such code:
$arr=array('a'=>$a);
but $a is not defined, so I get error. But if I write this code before
if (!isset($a))
$a=null;
all works. Why? In the beginning $a is not defined , so $a=null. or underfined!=NULL ?
When you write
array("a"=>$a)
it means that you want the key "a" refers to a variable reference named $a which does not exist in the first place thus you are getting an error; but when you add
$a=null;
before hand, although you are setting $a to null but actually you are creating a variable reference named $a that's known by PHP so there will be no errors.
Yes, in truth undefined != null, although the difference is only in the eyes of PHP when it decides whether to throw an error or not. Otherwise it's the same thing.
As you already discovered undefined is different from null.
Undefined means that the name $a in not yet into the scope of your function/code.
$a=null is a (no)value assigned to a variable.
By the way you should get a Notice, not an error as using undefined variables as right-values php warns you about a probable typo and proceed with the script execution.
As a rule of thumb, if you address an undefined variable on the left of the assignment (=) simbol (a left-value name) then php create a new variables name and bind it into the current scope, if the reference is on the right (are you using the value contained instead than the variable itself) php warns you and keep going. You can change this behaviour by the error_reporting function.
see the manual of isset
Returns TRUE if var exists and has
value other than NULL, FALSE
otherwise.