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 character string is opened with a certain word.
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 instance code is as follows:
- $ 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. then, the code is modified as follows.
The instance code is as follows:
- 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 instance code is as follows:
- If (strpos ($ title, $ keyword) === 0 ){
- Echo "correct ";
- } Else if (strpos ($ title, $ keyword) === false ){
- Echo "error ";
- }
Output: Error
The instance code is as follows:
-
- /*
- * Test boolean
- * 0 false
- */
- $ Num = 0;
- $ BTest1 = false;
- $ BTest2 = true;
- $ StrTest2 = 'false ';
- If ($ num = $ bTest1)
- {
- Echo ('digits 0 and false are equal '); // Display
- Echo ("
");
- }
- If ($ bTest1)
- {
- Echo ('never executed
'); // Not displayed
- }
- If (1)
- {
- Echo ('will it be executed,
'); // Execute
- }
- If ($ bTest2)
- {
- Echo ('I am the boss, I want to execute
'); // Execute
- }
- Else {
- Echo ('none of them belong to me.
');
- }
- 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 check the strong type of operators.