PHP Beginnings Ex. #7: Variable Data Types

PHP allows several different types of variables. For this PHP exercise, you will create one variable and assign it different values, then test its type for each value.

Write a script using one variable “$whatsit” to print the following to the browser. Your echo statements may include no words except “Value is”. In other words, use the function that will output the variable type to get the requested text. Use simple HTML to print each statement on its own line and add a relevant title to your page. Include line breaks in your code to produce clean, readable HTML.

Value is string.
Value is double.
Value is boolean.
Value is integer.
Value is NULL.



Your script should look similar to the following. Of course, the values you assigned to your variables are unlikely to be the same, but using the gettype() function, the output will match as long as the data types are correct.

The \n creates a line feed. It doesn't affect the output to the browser, but it does make the source code easier to read. If you leave it out and look at the HTML source in your browser, you will see that the lines all run together.

  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 Data Types</title> 
  7. </head>
  9. <body>  
  10. <h2>PHP Variable Data  Types</h2> 
  11. <p>
  12. <?php   
  14. $whatsit = 'George'; 
  15. echo "Value is ".gettype($whatsit).".<br/>\n";   
  17. $whatsit = 88.9; 
  18. echo "Value is ".gettype($whatsit).".<br/>\n";   
  20. $whatsit = true; 
  21. echo "Value is ".gettype($whatsit).".<br/>\n";   
  23. $whatsit = 8; 
  24. echo "Value is ".gettype($whatsit).".<br/>\n";
  26. $whatsit = null; 
  27. echo "Value is ".gettype($whatsit).".<br/>\n";   
  29. ?>   
  30. </p>
  31. </body> 
  32. </html>

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.


You can also use the unset($whatisit) function to get a value of NULL in the final line.

		$whatisit = "now it's a string";
		echo 'Value is ' . gettype($whatisit) . "<br />";
		$whatisit = 3.1415926;
		echo 'Value is ' . gettype($whatisit) . "<br />";
		$whatisit = true;
		echo 'Value is ' . gettype($whatisit) . "<br />";
		$whatisit = 98;
		echo 'Value is ' . gettype($whatisit) . "<br />";
		echo 'Value is ' . gettype($whatisit) . "<br />";

Because if you did PHP will read $whatsit as not existing variable and give you undefined variable error but still it will give Value is NULL at the end.

\n is for new line right?

Why doesn't it take me to the next line unlike when I just use ?

Also, how can I make float to appear instead of double?

With \n line, php will render a cleaner html code.


echo "Hello" ;
echo "world!";

would make a single line with the following html code "Hello world"

if you use \n, the html render would be render in two lines like:



I don't understand yet the use of \n. I created my code only with and it seems to be working fine...

            echo "Value is ", gettype ($whatsit), "<br>";
            echo 'Value is ', gettype ($whatsit), '<br>';
            echo "Value is ", gettype ($whatsit), "<br>";
            echo "Value is ", gettype ($whatsit), "<br>";
            echo "Value is ", gettype ($whatsit), "<br>";