불린(Booleans)

가장 간단한 유형입니다. bool은 진리값을 나타냅니다. 참(TRUE)이거나 거짓(FALSE)일 수 있습니다.

구문

bool 리터럴을 지정하려면 true 또는 false 상수를 사용하십시오. 둘 다 대소문자를 구분하지 않습니다.

                  
<?php
$foo = True; // assign the value TRUE to $foo
?>
                  
                

일반적으로 bool 값을 반환하는 연산자의 결과는 제어 구조로 전달됩니다.

                  
<?php
// == is an operator which tests
// equality and returns a boolean
if ($action == "show_version") {
    echo "The version is 1.23";
}

// this is not necessary...
if ($show_separators == TRUE) {
    echo "<hr>\n";
}

// ...because this can be used with exactly the same meaning:
if ($show_separators) {
    echo "<hr>\n";
}
?>
                  
                
부울로 변환

값을 bool로 명시적으로 변환하려면 (bool) 또는 (boolean) 형변환을 사용하십시오. 그러나 연산자, 함수 또는 제어 구조에 bool 인수가 필요한 경우 값이 자동으로 변환되기 때문에 대부분의 경우 캐스트가 필요하지 않습니다.

유형 저글링을 참조하십시오.

bool로 변환할 때 다음 값은 false로 간주됩니다.

  • the boolean false itself
  • the integer 0 (zero)
  • the floats 0.0 and -0.0 (zero)
  • the empty string, and the string "0"
  • an array with zero elements
  • the special type NULL (including unset variables)
  • 속성이 없는 빈 요소, 즉 자식도 속성도 없는 요소에서 생성된 SimpleXML 개체입니다.

다른 모든 값은 true로 간주됩니다(모든 리소스NAN 포함).

경고 -1은 다른 0이 아닌(음수이든 양수이든) 숫자와 마찬가지로 true으로 간주됩니다!

                  
<?php
var_dump((bool) "");        // bool(false)
var_dump((bool) "0");       // bool(false)
var_dump((bool) 1);         // bool(true)
var_dump((bool) -2);        // bool(true)
var_dump((bool) "foo");     // bool(true)
var_dump((bool) 2.3e5);     // bool(true)
var_dump((bool) array(12)); // bool(true)
var_dump((bool) array());   // bool(false)
var_dump((bool) "false");   // bool(true)
?>