Home > Backend Development > PHP Tutorial > How to distinguish false from 0 in php_PHP tutorial

How to distinguish false from 0 in php_PHP tutorial

WBOY
Release: 2016-07-13 17:14:29
Original
1461 people have browsed it

When a variable is 0 in PHP, the variable is also "equal" to false. So how to distinguish between 0 and false? This is very useful in some conditional statements, as this article will illustrate.

Look at the code first:

The function of this code is to find whether a string begins with a certain word

The code is as follows Copy code
 代码如下 复制代码

$title = "Hello world.";

$keyword = "you";

if(strpos($title , $keyword ) == 0) {

    echo "正确";

} else {

    echo "错误";

}

输出:正确

$title = "Hello world.";

$keyword = "you";
 代码如下 复制代码

if(strpos($title , $keyword ) == 0) {

    echo "正确";

} else if(strpos($title , $keyword ) == false) {

    echo "错误";

}

输出:正确

if(strpos($title , $keyword ) == 0) {

 代码如下 复制代码

if(strpos($title , $keyword ) === 0) {

    echo "正确";

} else if(strpos($title , $keyword ) === false) {

    echo "错误";

}

输出:错误

echo "correct";

} else {
 代码如下 复制代码

/*
* 测试boolean
* 0 false
*/
$num = 0;
$bTest1 = false;
$bTest2 = true;
$strTest2 = 'false';
if($num == $bTest1)
{
echo ('数字0与false是可以相等的');//显示
echo ("
");
 }
 if($bTest1)
 {
  echo('永不执行啊
');//不显示
 }
 if(1)
 {
  echo('会执行吗,
');//执行
 }
 if($bTest2)
 {
  echo('我是老大我要执行
');//执行
  
 }
 else{
  echo('不要的都归我了哦
');
 }
 echo (false == 0);//显示1 表示相等
 echo (true == 1);//显示1 表示相等
function testReturn ()
{
 echo('aaaaa');
 return;
 return 'bbbb';
 echo('cccc');
}
//return 表示该函数的返回就是执行到这下面的都将不执行了,exit是推出程序
echo testReturn();//调用该函数 将输出 ‘aaaa' ’bbbbb'
?>

echo "Error"; } Output: Correct
It seems that the code is correct? Why is the result wrong? Check the help manual and see that when the strpos() function searches for a word in a string, if the word exists, it returns the index position of the word, otherwise it returns false, so the code is modified as follows.
The code is as follows Copy code
if(strpos($title , $keyword ) == 0) { echo "correct"; } else if(strpos($title , $keyword ) == false) { echo "Error"; } Output: Correct
Why is it wrong again? It turns out that when a variable is 0 in PHP, the variable is also "equal" to false. So how to distinguish between 0 and false? It’s actually very simple, just modify the code:
The code is as follows Copy code
if(strpos($title , $keyword ) === 0) { echo "correct"; } else if(strpos($title , $keyword ) === false) { echo "Error"; } Output: Error
Example
The code is as follows Copy code
/*
* Test boolean
* 0 false
*/
$num = 0;
$bTest1 = false;
$bTest2 = true;
$strTest2 = 'false';
if($num == $bTest1)
{
echo ('The numbers 0 and false can be equal');//Display
echo ("
");
}
if($bTest1)
{
echo('Never executed
');//Does not display
}
if(1)
{
echo('Will it be executed?
');//Execute
}
if($bTest2)
{
echo('I am the boss and I want to execute
');//Execute

}
else{
echo('Everything you don’t want is mine
');
}
echo (false == 0);//Display 1 to indicate equality
echo (true == 1);//Display 1 to indicate equality
function testReturn ()
{
echo('aaaaa');
return;
return 'bbbb';
echo('cccc');
}
//return means that the return of this function means that everything below will not be executed, and exit means to exit the program
echo testReturn();//Calling this function will output ‘aaaa’ ‘bbbbb’
?>

Many times false is also equal to 0. When the value we want to return contains 0, for example, we should pay attention to the query of numbers. You can use === to determine whether they are completely equal,

PHP has a gettype() function to get the type of a variable. You can use the === operator (look, there are three equal signs). The difference from the == operator is that this operator compares both the value and type of the data.

When different variable types are involved in the termination condition, it is important to perform strong type checking by using the === and !== operators.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/628991.htmlTechArticleWhen a variable is 0 in PHP, the variable is also "equal" to false, so how to distinguish between 0 and false ? This is very useful in some conditional statements, as this article will illustrate. First look at the code: ...
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template