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.

 

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