is_bool

(PHP 4, PHP 5, PHP 7)

is_bool변수가 논리형인지 확인

설명

bool is_bool ( mixed $var )

주어진 변수가 논리형인지 확인합니다.

인수

var

평가할 변수.

반환값

varboolean이면 TRUE, 아니면 FALSE를 반환합니다.

예제

Example #1 is_bool() 예제

<?php
$a 
false;
$b 0;

// $a는 논리형이기 때문에, true입니다.
if (is_bool($a)) {
    echo 
"논리형입니다.";
}

// $b는 논리형이 아니기 때문에, true가 아닙니다.
if (is_bool($b)) {
    echo 
"논리형입니다.";
}
?>

참고

  • is_float() - 변수의 자료형이 소수인지 확인합니다
  • is_int() - 변수의 자료형이 정수인지 확인합니다
  • is_string() - 변수의 자료형이 문자열인지 확인합니다
  • is_object() - 변수가 객체인지 확인합니다
  • is_array() - 변수가 배열인지 확인

add a note add a note

User Contributed Notes 2 notes

up
12
phil
5 years 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
11
Julio Marchi
5 years ago
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 is a simple function that checks the status of the received variable in regard 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 the thing from 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.
To Top