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"



Here's one script that produces this output:

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"     
  2. "">   
  3. <html xmlns=""  xml:lang="en" lang="en"> 
  4. <head> 
  5. <meta http-equiv="content-type" content="text/html;charset=iso-8859-1" />
  6. <title>Variable Content and Destruction</title> 
  7. </head>   
  9. <body>   
  11. <?php   
  13. $name='Harry'; 
  14. $age=28;   
  16. var_dump($name); 
  17. echo "<br/>";   
  19. print_r($name); 
  20. echo "<br/>";   
  22. var_dump($age); 
  23. echo "<br/>";  
  25. $name=null;
  26. //In PHP versions up to 5.3.3, this statement could also have 
  27. //been written: unset($name);, and the next command would work correctly. 
  28. //More recent versions return an undefined variable error 
  29. //for var_dump after the variable is unset.
  31. var_dump($name);
  32. //The last two statements would have worked just as well with the $age variable.
  34. ?> 
  36. </body> 
  37. </html>

Var_dump() gives the type, size and value of the variable, whereas print_r() gives only the value.

See the output of the script in a separate window here. You can also view the output's HTML source from the new window, if you need to check that.

To open a PHP code editor in a new tab, click here.


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 );

also used error handing error_reporting ();