PHP 布尔值

WBOY
发布: 2024-08-29 12:38:25
原创
833 人浏览过

在了解什么是 PHP 布尔值之前,我们先来了解一下什么是布尔值?

开始您的免费软件开发课程

网络开发、编程语言、软件测试及其他

布尔值是一种用于大多数计算机编程语言(如 Java、Python、C、PHP 等)的数据类型。它是一种具有一个或两个可能值(true 或 false)的数据类型。它旨在表示逻辑和布尔代数的两个真值。根据条件,它将其值设置为 1(真)或 0(假)。许多编程语言都使用此数据类型来检查条件是否满足以及语句是否被执行。

PHP 布尔值

在 PHP 中,布尔数据类型用于设置变量的值。它主要用于条件语句,如 If、While、For、Switch 等。这些条件和迭代命令主要是为了测试这些布尔值表达式而定义的。布尔值仅返回两个值,即 true 或 false。因此,它用于条件语句中,以通过该特定条件来执行与其对应的以下语句。

PHP 布尔值的类型

让我们看一下不同类型的布尔值:

  • 整数: 该布尔值用于检查变量的输出是否非零的条件。如果输出为零,则条件为 false,并且当前不会在循环内执行语句,并将跳过循环并执行进一步的语句。
  • 浮点:此布尔值用于检查变量的输出是否为浮点数的条件,例如0.0。如果输出非零,则条件为真,将执行循环语句,如果输出为零,则将跳过循环内的语句并继续执行进一步的语句。
  • 字符串:此布尔值用于检查字符串是否为空如果条件语句的输出为 true,则输出将为字符串值,并且循环内的语句将被执行。如果输出为 false,则输出为零字符串或空字符串。
  • 数组: 这个布尔值用于检查数组中是否有元素。如果条件为真,则它必须至少具有一个元素,并且循环内的语句将被执行。如果条件为 false,则输出必须是空数组,并且将跳过循环内的语句并继续执行进一步的语句。
  • NULL:此布尔值用于检查变量的值是否为 NULL。如果变量在开始时就被初始化为 NULL,或者尚未设置任何值或未设置,则变量的值将为 NULL。如果条件为真,则循环内的语句将被执行。如果输出为 false,它将跳过循环内的语句并继续执行进一步的语句。
  • 对象:此布尔值用于检查对象是否存在。如果存在,则条件为真,将执行语句;条件为假,则将跳过循环内的语句,并继续执行进一步的语句。

布尔值如何工作?

布尔值只不过是 0 和 1,即 true 或 false。如果条件满足,则为 true,否则为 false。

示例#1

让我们考虑简单的示例来了解布尔值的工作原理。

代码:

<?php
$selling_price = 75.5;
$cost_price =50;
if ($selling_price == 0)
{
echo "The selling price should be a non zero";
}
else
{
echo "The selling price is $selling_price";
}
?>
登录后复制

输出:

PHP 布尔值

在上面的示例中,输出是非零。因此,if语句里面的语句不会被执行。

示例#2

让我们再举一个字符串布尔值的例子:

代码:

<?php
$a="Leela";
$b="Swamy";
if ($a)
{
echo "The name of the person is ".$a.$b;
}
else
{
echo "The string is empty";
}
?>
登录后复制

输出:

PHP 布尔值

在上面的示例中,名称非空,也没有使用比较运算符。 PHP 会自动转换该值并将其设置为其布尔值 true。因此,这些语句将被执行在 if 语句中。

示例 #3

我们再举个例子:

代码:

<?php
$var=NULL;
$var1=500;
if ($var == NULL)
{
$var=$var1;
echo "The value of var is $var";
}
?>
登录后复制

输出:

PHP 布尔值

在上面的示例中,$var 变量已初始化为 null。因此条件变为 true,并且执行循环内写入的语句并将值设置为 500。

Example #4

The function is_bool () can be used to check whether a variable contains a Boolean value or not. The is_bool () is an inbuilt function in PHP. It is a Boolean function so it returns TRUE when the value is a Boolean value, otherwise FALSE. Let’s take a simple example.

Code:

<?php
$a=TRUE;
$b=FALSE;
echo $a.'<br>';
echo $b.'<br>';
echo is_bool($a).'<br>';
echo is_bool ($b).'<br>';
?>
登录后复制

Output:

PHP 布尔值

In the above example, the output of the function is_bool() will be either 0 or 1. In this example, the output will be 1 and after the break also it will be 1. It just checks whether the Boolean value is set to the variable and in this example, both the variables have been initialized Boolean values.

Example #5

Similar to is_bool () function, we have a function called var_dump () function to print both the type as well as the value as output. This function is used to print the data type associated with the variable that the developers want to print on the screen.

Code:

<?php
$a = true;
echo $a.'<br>';
echo is_bool($a).'<br>';
var_dump($a).'<br>';
?>
登录后复制

Output:

PHP 布尔值

In the above example, the output of is_bool() function and var_dump function() differs. The var_dump() function will print the data type along with the value of the variable.

Conclusion

In this article, we discussed the Boolean value and its types. Many of the Boolean types are used in many programs and applications. Boolean values play a vital role in many programming languages especially in PHP where we use cookies and sessions to check whether they are set or unset.

以上是PHP 布尔值的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
php
来源:php
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!