Home Tutorial Php Phpbasics Tutorial PHP Boolean Data Types

 
 

PHP Boolean Data Types
Posted on: March 6, 2010 at 12:00 AM
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/>";

?>

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)

 

Related Tags for PHP Boolean Data Types:


Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.