search
HomeBackend DevelopmentPHP ProblemWhat is the php string boolean type?

What is the php string boolean type?

Jan 20, 2023 am 09:51 AM
phpBoolean type

php String Boolean type refers to the Boolean Boolean type. bool has only two values, which are used to express true values, either true or false. To specify a Boolean type, you can use the constant true or false, and its setting The syntax is "$foo = True;", which means setting "$foo" to "TRUE".

What is the php string boolean type?

The operating environment of this tutorial: Windows 10 system, PHP version 8.1, DELL G3 computer

What is the Boolean type of php string ?

Boolean Boolean type

bool has only two values, used to express the truth value, either true or false.

Syntax

To specify a bool, use the constant true or false. Both are not case sensitive.

<?php
$foo = True; // 设置 $foo 为 TRUE
?>

Normally the bool value returned by the operator will be passed to the control flow.

<?php
// == 是一个操作符,它检测两个变量是否相等,并返回一个布尔值
if ($action == "show_version") {
    echo "The version is 1.23";
}
// 这样做是不必要的...
if ($show_separators == TRUE) {
    echo "<hr>\n";
}
// ...因为可以使用下面这种简单的方式:
if ($show_separators) {
    echo "<hr>\n";
}
?>

Convert to Boolean

To explicitly convert a value to bool, you can use (bool) cast. Normally this is not necessary because the value will automatically be interpreted as a bool value when used in a logical context. Please read the Type Conversion page for more information.

See the identification of type conversion.

When converted to bool, the following values ​​are considered false:

  • The Boolean value false itself

  • Integer value 0 (zero)

  • Floating point value 0.0 (zero)-0.0 (zero)

  • Empty string "", and string "0"

  • An array that does not include any elements

  • Unit type NULL (including variables that have not been assigned a value)

  • The coercion behavior of internal objects is overloaded as bool. For example: a SimpleXML object created from an empty element with no attributes.

All other values ​​are considered true (including resources and NAN).

Warning:

-1, like other non-zero values ​​(positive or negative), is considered 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)
?>

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of What is the php string boolean type?. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools