Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does gettype() output "NULL" for an undefined variable?

Tags:

php

I have tried the following code:

<?php
    echo gettype($x);
?>

And I got the following output:

enter image description here

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?

like image 646
Steve Avatar asked May 29 '26 21:05

Steve


1 Answers

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".

like image 115
axiac Avatar answered Jun 01 '26 11:06

axiac



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!