boolval()

Description

Returns the Boolean value of an expression.

Syntax


boolean boolval( mixed $expr )

Parameters

$expr
expression for which boolean value is returned

Return Value

boolean
One of the following values will be returned:

  • true if $var evaluates to a Boolean true
  • false if $var evaluates to a Boolean false

Examples

1. Evaluate All Types as Boolean

<?php
echo 'boolval( 1 )           : ' . ( boolval( 1 )            ? 'true' : 'false' ) . "\n";
echo 'boolval( 6 )           : ' . ( boolval( 6 )            ? 'true' : 'false' ) . "\n";
echo 'boolval( -3 )          : ' . ( boolval( -3 )           ? 'true' : 'false' ) . "\n";
echo 'boolval( 0 )           : ' . ( boolval( 0 )            ? 'true' : 'false' ) . "\n";
echo 'boolval( 1.0 )         : ' . ( boolval( 1.0 )          ? 'true' : 'false' ) . "\n";
echo 'boolval( 2.3 )         : ' . ( boolval( 2.3 )          ? 'true' : 'false' ) . "\n";
echo 'boolval( -2.3 )        : ' . ( boolval( -2.3 )         ? 'true' : 'false' ) . "\n";
echo 'boolval( 0.0 )         : ' . ( boolval( 0.0 )          ? 'true' : 'false' ) . "\n";
echo 'boolval( "1" )         : ' . ( boolval( "1" )          ? 'true' : 'false' ) . "\n";
echo 'boolval( "0" )         : ' . ( boolval( "0" )          ? 'true' : 'false' ) . "\n";
echo 'boolval( "0.0" )       : ' . ( boolval( "0.0" )        ? 'true' : 'false' ) . "\n";
echo 'boolval( "abc" )       : ' . ( boolval( "abc" )        ? 'true' : 'false' ) . "\n";
echo 'boolval( "" )          : ' . ( boolval( "" )           ? 'true' : 'false' ) . "\n";
echo 'boolval( [1,2] )       : ' . ( boolval( [1,2] )        ? 'true' : 'false' ) . "\n";
echo 'boolval( [] )          : ' . ( boolval( [] )           ? 'true' : 'false' ) . "\n";
echo 'boolval( new stdClass ): ' . ( boolval( new stdClass ) ? 'true' : 'false' ) . "\n";
echo 'boolval( NULL )        : ' . ( boolval( NULL )         ? 'true' : 'false' ) . "\n";
?>

The output below shows the return value of boolval() when passed expressions of various data types:

  • integer – 0 is false, everything else is true
  • float – 0.0 is false, everything else is true
  • string – “0” and null string are false, everything else is true (even “0.0”)
  • array – empty array is false, everything else is true
  • object – null is false, everything else is true
  • null – null is false
boolval( 1 )           : true
boolval( 6 )           : true
boolval( -3 )          : true
boolval( 0 )           : false
boolval( 1.0 )         : true
boolval( 2.3 )         : true
boolval( -2.3 )        : true
boolval( 0.0 )         : false
boolval( "1" )         : true
boolval( "0" )         : false
boolval( "0.0" )       : true
boolval( "abc" )       : true
boolval( "" )          : false
boolval( [1,2] )       : true
boolval( [] )          : false
boolval( new stdClass ): true
boolval( NULL )        : false

2. Example heading

Description of example….



See Also

References