39 of 410 menu

The boolval Function

The boolval function returns the boolean value (true or false) of a variable. It accepts one parameter - the value to be converted to boolean type.

Syntax

boolval(mixed $value): bool

Example

Convert various data types to boolean values:

<?php var_dump(boolval(0)); // false var_dump(boolval(1)); // true var_dump(boolval('')); // false var_dump(boolval('abc')); // true var_dump(boolval([])); // false var_dump(boolval([1, 2])); // true ?>

Example

Using the function with objects:

<?php class Test { public $prop = 1; } $obj = new Test(); var_dump(boolval($obj)); // true var_dump(boolval(new stdClass())); // true ?>

Code execution result:

true true

See Also

  • the intval function,
    which converts a value to an integer
  • the floatval function,
    which converts a value to a floating-point number
  • the strval function,
    which converts a value to a string
byenru