PHP: Detailed explanation of boolean data type examples
什么是布尔型(boolean)数据类型?
布尔型是PHP4种标量数据类型中的一个,它是PHP中较为常用的数据类型之一,他只能保存两个值true值和false值。它不区分大小写,所以true写成TRUE是一样的。
如果我们需要设定一个布尔型的变量,只需将true值或者false值直接赋值给变量就好,想下面这样,
$a=true; $b=false;
实例
通常布尔型变量是应用在条件控制或循环控制语句的表达式中。下面在if条件控制语句中判断变量$boo中的值是否为true,如果为true,则输出“变量$boo为真!”,否则输出“变量$boo为假!”,代码如下:
<?php header("Content-type:text/html;charset=utf-8"); //设置编码 $boo=true; //说明一个boolean类型变量,赋初值为true if($boo==true){ //判断$boo变量是否为真 echo "变量\$boo为真!"; }else{ echo "变量\$boo为假!"; } ?>
代码运行结果:
注意:在PHP中不是只有boolean值为假,在一些特殊情况下非boolean值也被认为是假。
这些特殊情况为:0,0.0,“0”,空白字符串(“”),只声明没有赋值的数组等,下面是这些特殊情况的示例
1. 变量的值为整型时,如果值为0时为假,其它为真。
<?php var_dump(1 == true); //bool(true) var_dump(0 == false); //bool(true) var_dump(-1 == false); //bool(false) var_dump(0 == true); //bool(false) ?>
2. 变量的值为浮点型时,如果值为0.00(即其中只有0)即为假,其它都为真:
<?php var_dump(1 == true); //bool(true) var_dump(0 == false); //bool(true) var_dump(-1 == false); //bool(false) var_dump(0 == true); //bool(false ?>
3. 变量值为字符串时,如果值为“”(即空)或0即为假,其它都为真(既使为0.00或“ ”中间有空格也是真):
<?php var_dump("" == false); //bool(true) var_dump("0" == false); //bool(true) var_dump(" " == true); //bool(true) var_dump("0.00" == true); //bool(true) ?>
4. 变量为数组型时,如果数组中没有任何成员,即为假,否则为真。
<?php var_dump(array() == ''); //bool(false) var_dump(array() === ''); //bool(false) var_dump(array() == NULL); //bool(true) var_dump(array() === NULL); //bool(false) var_dump(array() == 0); //bool(false) var_dump(array() === 0); //bool(false) var_dump(array() == false); //bool(true) var_dump(array() === false); //bool(false) var_dump(array() == true); //bool(false) var_dump(array() === true); //bool(false) ?>
上面就是对booeanl型数据变量在PHP中的常规用法。下一节,我们给大家介绍标量数据类型中的“string(字符串型)”。
The above is the detailed content of PHP: Detailed explanation of boolean data type examples. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



The methods of the php8 data type include converting strings to integers, converting integers to strings, converting strings to floating point numbers, converting floating point numbers to strings, converting arrays to strings, converting strings to arrays, and converting Boolean values to integers. Integer conversion to Boolean value and variable type determination and conversion. Detailed introduction: 1. Converting a string to an integer includes the intval() function and (int) forced type conversion; 2. Converting an integer to a string includes the strval() function and (string) forced type conversion; 3. Converting a string to a float Points and so on.

In a MySQL database, gender fields can usually be stored using the ENUM type. ENUM is an enumeration type that allows us to select one as the value of a field from a set of predefined values. ENUM is a good choice when representing a fixed and limited option like gender. Let's look at a specific code example: Suppose we have a table called "users" that contains user information, including gender. Now we want to create a field for gender, we can design the table structure like this: CRE

In MySQL, the most suitable data type for gender fields is the ENUM enumeration type. The ENUM enumeration type is a data type that allows the definition of a set of possible values. The gender field is suitable for using the ENUM type because gender usually only has two values, namely male and female. Next, I will use specific code examples to show how to create a gender field in MySQL and use the ENUM enumeration type to store gender information. The following are the steps: First, create a table named users in MySQL, including

Python data types are: 1. Integer type; 2. Floating point type; 3. Complex number; 4. Boolean type; 5. String; 6. List; 7. Tuple; 8. Set; 9. Dictionary. Detailed introduction: 1. Integer type, used to represent integers, which can be positive, negative, or zero. In Python, the range of values that integers can represent is platform-specific; 2. Floating point type, used to represent numbers with decimal parts. Numbers, floating point type can represent positive numbers, negative numbers and zero; 3. Complex numbers, used to represent complex numbers, including real and imaginary parts; 4. Boolean type, used to represent Boolean values, etc.

Python is widely used in a wide range of fields with its simple and easy-to-read syntax. It is crucial to master the basic structure of Python syntax, both to improve programming efficiency and to gain a deep understanding of how the code works. To this end, this article provides a comprehensive mind map detailing various aspects of Python syntax. Variables and Data Types Variables are containers used to store data in Python. The mind map shows common Python data types, including integers, floating point numbers, strings, Boolean values, and lists. Each data type has its own characteristics and operation methods. Operators Operators are used to perform various operations on data types. The mind map covers the different operator types in Python, such as arithmetic operators, ratio

When designing database tables, choosing the appropriate data type is very important for performance optimization and data storage efficiency. In the MySQL database, there is really no so-called best choice for the data type to store the gender field, because the gender field generally only has two values: male or female. But for efficiency and space saving, we can choose a suitable data type to store the gender field. In MySQL, the most commonly used data type to store gender fields is the enumeration type. An enumeration type is a data type that can limit the value of a field to a limited set.

Detailed explanation of how to use Boolean types in MySQL MySQL is a commonly used relational database management system. In practical applications, it is often necessary to use Boolean types to represent logical true and false values. There are two representation methods of Boolean type in MySQL: TINYINT(1) and BOOL. This article will introduce in detail the use of Boolean types in MySQL, including the definition, assignment, query and modification of Boolean types, and explain it with specific code examples. 1. The Boolean type is defined in MySQL and can be

C language is a widely used computer programming language that is efficient, flexible and powerful. To be proficient in programming in C language, you first need to understand its basic syntax and data types. This article will introduce the basic syntax and data types of C language and give examples. 1. Basic syntax 1.1 Comments In C language, comments can be used to explain the code to facilitate understanding and maintenance. Comments can be divided into single-line comments and multi-line comments. //This is a single-line comment/*This is a multi-line comment*/1.2 Keyword C language
