PHP Tutorial Data types - part 2

Register here to save your progress

? ?

Boolean Type

Boolean variables have a logical value. The boolean value can be wither TRUE or FALSE.

You can convert other types of values to boolean the cast operator (bool), but in general you won't be needed to use it, PHP will recognize it's logical values. Values like 0 or empty strings will automatically be converted to False, while strings or number that have other values than 0 will be converted to True.

These boolean values are case-insensitive for PHP, so you can either write TRUE, true or True, same with the FALSE.

<?php

$correct = true;

$still_correct = (bool)'Hello';
//this also has the value true;

?>

What is the boolean value of the number 25?

NULL Type

NULL are the variables that don't have any value assigned to them, it represents the absence of any value.

A variable can be NULL if:

  • it has been assign to the NULL value;
  • it hasn't been assign any value to it;
  • it's value has been removed using the function unset()
<?php


$name;
//it's null 

$address = null;
//it's null

$phone_number = '0226565333222';

unset($phone_number);
//it's null now

?>

How can you set a variable to NULL?