Wednesday, July 3, 2024
HomeLanguagesPhpPHP | boolval() Function

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')."\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

Nicole Veronica Rubhabha
Nicole Veronica Rubhabha
A highly competent and organized individual DotNet developer with a track record of architecting and developing web client-server applications. Recognized as a personable, dedicated performer who demonstrates innovation, communication, and teamwork to ensure quality and timely project completion. Expertise in C#, ASP.Net, MVC, LINQ, EF 6, Web Services, SQL Server, MySql, Web development,
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments