Bool false php что это
Перейти к содержимому

Bool false php что это

  • автор:

Булев

Это простейший тип. boolean выражает истинность значения. Он может быть либо TRUE , либо FALSE .

Синтаксис

Для указания boolean , используйте константы TRUE или FALSE . Обе они регистронезависимы.

Обычно, некоторый оператор возвращает boolean значение, которое потом передается управляющей конструкции.

<?php
// == это оператор, который проверяет
// эквивалентность и возвращает boolean
if ( $action == «show_version» ) <
echo «The version is 1.23» ;
>

// это необязательно.
if ( $show_separators == TRUE ) <
echo «<hr>\n» ;
>

// . потому что следующее имеет тот же самый смысл:
if ( $show_separators ) <
echo «<hr>\n» ;
>
?>

Преобразование в булев тип

Для явного преобразования в boolean , используйте (bool) или (boolean). Однако, в большинстве случаев приведение типа необязательно, так как значение будет автоматически преобразовано, если оператор, функция или управляющая конструкция требует boolean аргумент.

При преобразовании в boolean , следующие значения рассматриваются как FALSE :

  • само значение booleanFALSE
  • integer 0 (ноль)
  • float 0.0 (ноль)
  • пустая строка, и строка "0"
  • массив без элементов
  • объект без полей (только для PHP 4)
  • особый тип NULL (включая неустановленные переменные)
  • Объекты SimpleXML, созданные из пустых тегов

Все остальные значения рассматриваются как TRUE (включая любой resource).

-1 рассматривается как TRUE , как и любое другое ненулевое (отрицательное или положительное) число!

PHP Boolean

A boolean value represents a truth value. In other words, a boolean value can be either true or false . PHP uses the bool type to represent boolean values.

To represent boolean literals, you can use the true and false keywords. These keywords are case-insensitive. Therefore, the following are the same as true :

  • True
  • TRUE

And the following are the same as false :

  • False
  • FALSE

When you use non-boolean values in a boolean context, e.g., if statement. PHP evaluates that value to a boolean value. The following values evaluate to false :

  • The keyword false
  • The integer zero (0)
  • The floating-point number zero (0.0)
  • The empty string ( » ) and the string «0»
  • The NULL value
  • An empty array, i.e., an array with zero elements

PHP evaluates other values to true .

The following shows how to declare variables that hold Boolean values:

To check if a value is a Boolean, you can use the built-in function is_bool() . For example:

When you use the echo to show a boolean value, it’ll show 1 for true and nothing for false , which is not intuitive. To make it more obvious, you can use the var_dump() function. For example:

Bool false php что это

This is the simplest type. A boolean expresses a truth value. It can be either TRUE or FALSE .

Syntax

To specify a boolean literal, use the constants TRUE or FALSE . Both are case-insensitive.

Typically, the result of an operator which returns a boolean value is passed on to a control structure.

<?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» ;
>
?>

Converting to boolean

To explicitly convert a value to boolean , use the (bool) or (boolean) casts. However, in most cases the cast is unnecessary, since a value will be automatically converted if an operator, function or control structure requires a boolean argument.

When converting to boolean , the following values are considered FALSE :

  • the booleanFALSE itself
  • the integer 0 (zero)
  • the float 0.0 (zero)
  • the empty string, and the string "0"
  • an array with zero elements
  • the special type NULL (including unset variables)
  • SimpleXML objects created from empty tags

Every other value is considered TRUE (including any resource and NAN ).

-1 is considered TRUE , like any other non-zero (whether negative or positive) number!

PHP Boolean Data Type

This is one of the scalar data types in PHP. A boolean data can be either TRUE or FALSE. These are predefined constants in PHP. The variable becomes a boolean variable when either TRUE or FALSE is assigned.

Syntax

Result of echoing TRUE value displays 1 while for FALSE it shows nothing. Using var_dump() function displays bool as type with value

Boolean constants are not case sensitive. That means TRUE is equivalent to true and FALSE is similar to False

Logical operators return boolean value

Casting

Any data type can be explicitly converted to boolean with the help of casting operator (bool) or (boolean), although, most of the times, conversion is done automatically whenever required.

PHP Version

This description is applicable to all versions of PHP.

Following example shows use of echo and var_dump() to diplay boolean value

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *