Tutorialspoint.dev

PHP | boolval() Function

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')." ";
echo 'boolval of -3    : '.( boolval( -3 )? 'true' : 'false')." ";
echo 'boolval of 0: ' .( boolval( 0 )? 'true' : 'false')." ";
echo 'boolval of 3.5: '.( boolval( 3.5 )? 'true' : 'false')." ";
echo 'boolval of -3.5: '.( boolval( -3.5 )? 'true' : 'false' )." ";
echo 'boolval of 0.0: '.( boolval( 0.0 )? 'true' : 'false' )." ";
echo 'boolval of "1": '.( boolval( "1" )? 'true' : 'false' )." ";
echo 'boolval of "0": '.( boolval( "0" )? 'true' : 'false' )." ";
echo 'boolval of "0.0": '.( boolval( "0.0" )? 'true' : 'false' )." ";
echo 'boolval of "xyz": '.( boolval( "xyz" )? 'true' : 'false' )." ";
echo 'boolval of "": '.( boolval( "" )? 'true' : 'false' )." ";
echo 'boolval of [1, 5]: '.( boolval( [1, 5] )? 'true' : 'false' )." ";
echo 'boolval of []: '.( boolval( [] )? 'true' : 'false' )." ";
echo 'boolval of NULL: '.( boolval( NULL )? 'true' : 'false' )." ";
  
?>

/div>

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

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



This article is attributed to GeeksforGeeks.org

You Might Also Like

leave a comment

code

0 Comments

load comments

Subscribe to Our Newsletter