Follow:  Follow PHPExercises on Twitter  Follow us on Facebook

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. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">   
  3. <html xmlns="http://www.w3.org/1999/xhtml"  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>
  8.  
  9. <body>  
  10. <h2>PHP Variable Data  Types</h2> 
  11. <p>
  12. <?php   
  13.  
  14. $whatsit = 'George'; 
  15. echo "Value is ".gettype($whatsit).".<br/>\n";   
  16.  
  17. $whatsit = 88.9; 
  18. echo "Value is ".gettype($whatsit).".<br/>\n";   
  19.  
  20. $whatsit = true; 
  21. echo "Value is ".gettype($whatsit).".<br/>\n";   
  22.  
  23. $whatsit = 8; 
  24. echo "Value is ".gettype($whatsit).".<br/>\n";
  25.  
  26. $whatsit = null; 
  27. echo "Value is ".gettype($whatsit).".<br/>\n";   
  28.  
  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.

Comments

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


<?php
		
		$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 />";
		unset($whatisit);
		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.

f.ex.:

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:

"Hello
World"

Hi,

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

            $whatsit="around";
            echo "Value is ", gettype ($whatsit), "<br>";
            $whatsit=1.50;
            echo 'Value is ', gettype ($whatsit), '<br>';
            $whatsit=TRUE;
            echo "Value is ", gettype ($whatsit), "<br>";
            $whatsit=1;
            echo "Value is ", gettype ($whatsit), "<br>";
            $whatsit=NULL;
            echo "Value is ", gettype ($whatsit), "<br>";