PHP 7.3.12 Released

is_bool

(PHP 4, PHP 5, PHP 7)

is_bool Comprueba si una variable es de tipo booleano

Descripción

is_bool ( mixed $var ) : bool

Obtiene si la variable dada es un booleano.

Parámetros

var

La variable a ser evaluada.

Valores devueltos

Devuelve TRUE si var es un boolean, FALSE de lo contrario.

Ejemplos

Ejemplo #1 Ejemplos de is_bool()

<?php
$a 
false;
$b 0;

// Ya que $a es un booleano, devolverá true
if (is_bool($a) === true) {
    echo 
"Sí, este es un booleano";
}

// Ya que $b no es un booleano, devolverá false
if (is_bool($b) === false) {
    echo 
"No, este no es un booleano";
}
?>

Ver también

  • is_float() - Comprueba si el tipo de una variable es float
  • is_int() - Comprueba si el tipo de una variable es integer
  • is_string() - Comprueba si una variable es de tipo string
  • is_object() - Comprueba si una variable es un objeto
  • is_array() - Comprueba si una variable es un array

add a note add a note

User Contributed Notes 3 notes

up
7
phil
10 months ago
It should be stated that this function returns true if the _type_ of it's argument is boolean. It does not convert or coerce the value to a boolean type, not sure why so many comments focus on how to do this.
However, if you arrived here looking for a solution to convert a value to a boolean type, use this:

to_bool($x) { return (bool)$x; }
up
0
Julio Marchi
2 months ago
The function Michael Smith published doesn't do the work as it should.

To check if a variable is boolean is one thing, to evaluate if the value of a variable represents a boolean condition (true or false) is another.

Here a simple function that checks the status of the received variable in regards to boolean equivalencies (case insensitive).

<?php
/**
* Check "Booleanic" Conditions :)
*
* @param  [mixed]  $variable  Can be anything (string, bol, integer, etc.)
* @return [boolean]           Returns TRUE  for "1", "true", "on" and "yes"
*                             Returns FALSE for "0", "false", "off" and "no"
*                             Returns NULL otherwise.
*/
function is_enabled($variable)
{
    if (!isset(
$variable)) return null;
    return
filter_var($variable, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE);
}
?>

Of course, it is a simplistic approach, but for the majority of cases it will do the job right.

And, just to put thing in the right perspective, here's a real function that does what Phill disclosed:

<?php
/**
* Convert $variable to boolean (adapted from Phill answer)
*
* @param  [mixed]  $variable  Can be anything
* @return [boolean]           Returns the booelan equivalent to $variable based on Zend Enegine interpretation
*/
function to_bool($variable)
{
    return (bool)
$variable;
}
?>

I hope it helps someone. Happy coding.
up
-5
Michael Smith
6 years ago
The function in the last comment also converts the string 'false' to a true boolean. Here's a function that does what I think was intended:

<?php
function toBool($var) {
  if (!
is_string($var)) return (bool) $var;
  switch (
strtolower($var)) {
    case
'1':
    case
'true':
    case
'on':
    case
'yes':
    case
'y':
      return
true;
    default:
      return
false;
  }
}
To Top