Follow:  Follow PHPExercises on Twitter  Follow us on Facebook

PHP Beginnings Ex. #5: Variable Content and Destruction

When you are writing scripts, you will often need to see exactly what is inside your variables. For this PHP exercise, think of the ways you can do that, then write a script that outputs the following, using the echo statement only for line breaks.

string(5) "Harry"



Why does your output not show this error:

Notice: Undefined variable: name in C:\wamp\www\PHP-Exercises\Beginnings.php on line 66

Thanks for the question. But we need more information to be able to help you. As you can see, there is no line 66 in the answer script. What variable do you have on that line? There could be many reasons it's not working right. If you like, you could paste your script into a comment.

My output is exactly like yours, except for the error notice between int(28) and NULL. The error is produced by $name = NULL;.

We get the same output with:
and we don't get an error message.

What version of PHP are you running? Ours is PHP/5.3.3.

Not sure how else we can help.

Actually, it is unset($name); that causes the error notice, not $name = NULL;.

I too noticed the error. It occurred when running the script locally on my WAMP install running PHP version 5.3.4

But when I ran the exact same script on my web server (running PHP 5.2.15) the error did not occur, and the output was as expected.

PHP 5.3.5 also produced the error. We have changed the answer script to reflect the most recent versions of PHP.

Thank you for helping to resolve this issue!

This is from a comment in the php manual
The main difference between <?php unset($var);?> and <?php $var = null;?> is, that unset() will reset the state to $var to something like "not set at all".

// register_globals = Off
echo $var; // Notice

$var = null;
echo $var; // Nothing

echo $var; // Notice

unset and null are not the same thing. whether it throws a warning depends on your php.ini settings.

Hello, I am a beginner in PHP. I was just wondering why print_r was used when echo can be used?

also, how can I include PHP codes in this comments? thnks

Notice: Undefined variable: name in path/to/your/file
I believe this notice is because PHP thinks the variable is undefined because it has been set to null by the unset function.
To turn this off include this at the top of your file.

  1. error_reporting( error_reporting() & ~E_NOTICE );