PHP Boolean Data Types


 

PHP Boolean Data Types

In this PHP tutorial we will study about boolean data type of PHP Programming Language. By default value of a boolean data type variable is false. Few interesting results are shown with the help of examples, based on explicit conversion to boolean.

In this PHP tutorial we will study about boolean data type of PHP Programming Language. By default value of a boolean data type variable is false. Few interesting results are shown with the help of examples, based on explicit conversion to boolean.

PHP Boolean Data Type:

Boolean is one of the simplest data type supported by PHP. A boolean is used to express a truth value either true or false.

For conversion to boolean data type, we need to use (bool) or (boolean) casts. Though it is not necessary because whenever we need to get a boolean value from an operator, function or control structure requires a boolean value, it automatically converts.

Example of PHP Boolean Data Type :

<?php

$a=23;

var_dump ((boolean)($a));echo"<br/>";

$a=23.23;

var_dump ((boolean)($a));echo"<br/>";

$a=0.0;

var_dump ((boolean)($a));echo"<br/>";

$a=-12;

var_dump ((boolean)($a));echo"<br/>";

$a;

var_dump ((boolean)($a));echo"<br/>";

$a=NULL;

var_dump ((boolean)($a));echo"<br/>";

$a=0;

var_dump ((boolean)($a));echo"<br/>";

$a='Hello';

var_dump ((boolean)($a));echo"<br/>";

$a='a';

var_dump ((boolean)($a));echo"<br/>";

$a=(array(1,2));

var_dump ((boolean)($a));echo"<br/>";

$a=(array());

var_dump ((boolean)($a));echo"<br/>";

$a=("");

var_dump ((boolean)($a));echo"<br/>";

?>

0

Output:

bool(true)
bool(true)
bool(false)
bool(true)
bool(true)
bool(false)
bool(false)
bool(true)
bool(true)
bool(true)
bool(false)
bool(false)

 

Ads