Open In App

PHP | boolval() Function

Last Updated : 01 Jul, 2019
Improve
Improve
Like Article
Like
Save
Share
Report

The boolval() function is an inbuilt function in PHP which gives the Boolean value for a given expression.

Syntax:

boolean boolval( $expr )

Parameter: This function accepts only one parameter as shown in above syntax and described below:

  • $expr: The expression or the scalar which you want to change into boolean value. It can be a string type, integer type and etc.

Return Value: This function will return a boolean value based on the below conditions.

  • if $expr is evaluated to boolean true it will return TRUE.
  • if $expr is evaluated to boolean false it will return FALSE.

Below is the list of different variable types along with their values which will evaluate to TRUE or FALSE when converted to boolean value:

  • integer – in this 0 is false and everything else is true.
  • float – in this 0.0 is false and everything else is true.
  • string – “0” and null string are false and everything else is true (even “0.0”)
  • array – empty array is false and everything else is true
  • object – here null is false and everything else is true
  • null – null is always false.

Below program illustrate the boolval() function in PHP:




<?php
// PHP program to illustrate 
// the boolval() function
  
echo 'boolval of 3: '.( boolval( 3 )? 'true' : 'false')."\n";
echo 'boolval of -3    : '.( boolval( -3 )? 'true' : 'false')."\n";
echo 'boolval of 0: ' .( boolval( 0 )? 'true' : 'false')."\n";
echo 'boolval of 3.5: '.( boolval( 3.5 )? 'true' : 'false')."\n";
echo 'boolval of -3.5: '.( boolval( -3.5 )? 'true' : 'false' )."\n";
echo 'boolval of 0.0: '.( boolval( 0.0 )? 'true' : 'false' )."\n";
echo 'boolval of "1": '.( boolval( "1" )? 'true' : 'false' )."\n";
echo 'boolval of "0": '.( boolval( "0" )? 'true' : 'false' )."\n";
echo 'boolval of "0.0": '.( boolval( "0.0" )? 'true' : 'false' )."\n";
echo 'boolval of "xyz": '.( boolval( "xyz" )? 'true' : 'false' )."\n";
echo 'boolval of "": '.( boolval( "" )? 'true' : 'false' )."\n";
echo 'boolval of [1, 5]: '.( boolval( [1, 5] )? 'true' : 'false' )."\n";
echo 'boolval of []: '.( boolval( [] )? 'true' : 'false' )."\n";
echo 'boolval of NULL: '.( boolval( NULL )? 'true' : 'false' )."\n";
  
?>


Output:

boolval of 3: true
boolval of -3    : true
boolval of 0: false
boolval of 3.5: true
boolval of -3.5: true
boolval of 0.0: false
boolval of "1": true
boolval of "0": false
boolval of "0.0": true
boolval of "xyz": true
boolval of "": false
boolval of [1, 5]: true
boolval of []: false
boolval of NULL: false

Reference:
http://http://php.net/manual/en/function.boolval.php



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads