What does the unassigned static variable in PHP contain and why it prints blank space in a browser output window? -
i've following code snippet, please have close on :
<!doctype html> <html> <body> <?php function mytest() { static $x; echo $x; $x++; } mytest(); echo "<br>"; mytest(); echo "<br>"; mytest(); ?> </body> </html>
note : name of file contains above code demo.php , location on laptop c:\xampp\htdocs\php_playground\demo.php
the output received when run above program browser hitting url http://localhost/php_playground/demo.php
below :
1 2
the screenshot of same attached, please have @ it.
- my question why first line in output blank containing white space only?
- why it's not printing 0 or word "null" or "empty" that?
- what unassigned static variable in php contain?
- does default value of static unassigned variable , default value of unassigned normal variable differ?
please give me suitable answer proper explanation.
an unassigned variable in php cast null
.
<?php echo null; ?>
doesn't echo anything, since null
has no value.
from null
page of php documentation (emphasis mine) :
the special null value represents variable no value. null possible value of type null.
a variable considered null if:
it has been assigned constant null.
it has not been set value yet.
it has been unset().
try replacing echo $x;
var_dump($x)
in snippet, output following :
null int(1) int(2)
Comments
Post a Comment