How is the difference between false and 0 in php?

Source: Internet
Author: User

In PHP, when the variable is 0, the variable is "equal to" false "at the same time. How can we distinguish between 0 and false? This is useful in some condition statements. This article will give an example.

First, check the Code:

The function of this Code is to find whether a string starts with a word.

The Code is as follows: Copy code

$ Title = "Hello world .";

$ Keyword = "you ";

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

Echo "correct ";

} Else {

Echo "error ";

}

Output: Correct

It seems that the code is correct? Why is the result incorrect? View the help manual. When the strpos () function finds a word in a string, if the word exists, the index position of the word is returned. Otherwise, false is returned. 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? In PHP, when the variable is 0, the variable is "equal to" false "at the same time. How can we distinguish between 0 and false? In fact, it is very easy to 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

<? Php
/*
* Test boolean
* 0 false
*/
$ Num = 0;
$ BTest1 = false;
$ BTest2 = true;
$ StrTest2 = 'false ';
If ($ num = $ bTest1)
{
Echo ('digits 0 and false are equal '); // display
Echo ("<br/> ");
}
If ($ bTest1)
{
Echo ('never execute <br/> '); // not displayed
}
If (1)
{
Echo ('will it be executed? <br/>'); // Execute
}
If ($ bTest2)
{
Echo ('I am the boss, I want to execute <br/>'); // Execute

}
Else {
Echo ('none of them belong to me. <br/> ');
}
Echo (false = 0); // 1 indicates equal
Echo (true = 1); // display 1 to indicate equal
Function testReturn ()
{
Echo ('aaaaa ');
Return;
Return 'bbbbbb ';
Echo ('cccccc ');
}
// Return indicates that the return of the function is not executed until it is executed, and exit is the launch program.
Echo testReturn (); // call this function to output 'aaa' 'bbbbbbb'
?>

In many cases, false is equal to 0. When we want to return a value containing 0, for example, we need to pay attention to the number query. We can use ===to determine whether it is completely equal,

PHP has a gettype () function to obtain the variable type. You can use the = Operator (after reading it, there are three equal signs ). The difference between the = Operator and the = Operator is that the operator compares the values and types of data at the same time.
 
When different variable types are involved in the termination condition, use = and! = It is very important to perform a strong type check for operators.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.