Ask a small white question: why PHP is often used! ==false true, why don't you just use ===true?

Source: Internet
Author: User
Tags strtok
For example, the following code:

$str4="php-class,mysql-class,div-css,0,dreamweaver";$s=strtok($str4,',');while($s!==false){    echo $s."
"; $s=strtok(',');}

while ($s ===true) {} Why can't it be displayed?
True where to use it ~ ~
Beginners Please care O (∩_∩) o~
Happy New Year ~

My own understanding is:! ==false will satisfy 1. The value is not equal to 2. The type does not equal, when the strtok decomposition ends, the return of NULL (the Mongolian) appears $s=false The loop statement is not true, but if you use ===true, you also need to satisfy: 1. The value equals 2. Type Equals, Then the first decomposed string is not a Boolean value, so the loop stops directly, but if you encounter ' 0 ' when using ' = = ', (not equal to true)!=ture, then the loop stops at this point.
Use! ==false means that the value is not equal, the type is not equal, and only false is returned to stop
Tried, seemingly right, but such a complex logic light would have to be 5 minutes, brain chaos .....

Reply content:

For example, the following code:

$str4="php-class,mysql-class,div-css,0,dreamweaver";$s=strtok($str4,',');while($s!==false){    echo $s."
"; $s=strtok(',');}

while ($s ===true) {} Why can't it be displayed?
True where to use it ~ ~
Beginners Please care O (∩_∩) o~
Happy New Year ~

My own understanding is:! ==false will satisfy 1. The value is not equal to 2. The type does not equal, when the strtok decomposition ends, the return of NULL (the Mongolian) appears $s=false The loop statement is not true, but if you use ===true, you also need to satisfy: 1. The value equals 2. Type Equals, Then the first decomposed string is not a Boolean value, so the loop stops directly, but if you encounter ' 0 ' when using ' = = ', (not equal to true)!=ture, then the loop stops at this point.
Use! ==false means that the value is not equal, the type is not equal, and only false is returned to stop
Tried, seemingly right, but such a complex logic light would have to be 5 minutes, brain chaos .....

PHP is a weakly typed language, the most important reason for weakly typed languages: implicit type conversions.

The main performance is assignment, calculation and comparison:
Assignment value:

$a = 1; // int$a = '1'; // string

The type of $a in the example above is changed with the type of assignment, and you know that this is not possible in C, because the variable type in C is determined when it is declared and cannot be changed after it is determined.

Calculation:

$a = 1;$b = '2';echo $a + $b; // 结果是 3

Similar code does not work in Python, and you get an error like this:

TypeError: unsupported operand type(s) for +: 'int' and 'str'

Python is also a strongly typed language and does not infer the type of the variable, so it throws the wrong one directly. But how does PHP do it?
The connection string in Python is also used + , but PHP needs to be used . . Inside PHP, when you use it + , the variables on both sides of the symbol are first converted to a numeric type (floating point, Integer), and in the same vein, . when used, the symbols are actually converted to strings first. Because Python does not need to be converted, it encounters a numeric calculation and encounters a string connection.

This conversion process is also interesting:

$a = 1;$b = '2';echo $a + $b; // 3$b = 'a2';echo $a + $b; // 1$b = '2a';echo $a + $b; // 3

See above you should have guessed it: the string is searched backwards until a non-numeric character is encountered.

Here's what we're going to encounter here: comparisons.

==And === include != and !== the difference in fact you have guessed that one will compare the type, one will not compare the type. In the internal description of PHP equal and identical These two words to describe. Feel the difference.

The exact order is: === and !== is the first to determine whether the same type, and then compare the specific value. If the types are different, there is no need to continue comparing them. This means that even the 1.0 === 1 obtained will be false , because the types are different.

In fact == , it is to check the type, but the action is: it will be based on the operator on both sides of the variable type of case to make a judgment on the variable first implicit conversion and then compare! Here I don't tell you the order of conversions, but the basic ones you should know:

0 == false; // true'1' == 1; // truenull == false; // truenull != 'null'; // true

And what you may not know, such as:

123 == '123abc'; // true'0e123' == '0e456'; // true

The problem here is basically clear. Because '0' == false it is set up, this is the !== reason to use.

In strtok fact, it's actually a strange function, which is a function of its own iteration. If used != to judge, it is impossible to accurately remove the strtok('hello world 0', ' ') third paragraph of the decomposition 0 .

http://cn2.php.net/manual/zh/function.strtok.php

Some functions return a value of int or string type by default and only return if it fails false . So use when making judgments !==false . For example strtok , if the execution result is the desired result, return string otherwise false . so~~

Update a bit

Again, for example, a function that returns the position in which it is located, such as the one in which strpos $str it is found, $find 123 1 is returned int(0) . Only when not found, returnfalse

However, if (0) the result is false that the logic in the if will not be executed. So judging if ($result !== false) is the right way.

About !== and != , === and == the difference between the judgment, Google it yourself.

'something' !== false 'something' !== true

Both of these expressions are true

My own understanding is:! ==false will satisfy 1. The value is not equal to 2. The type does not equal, when the strtok decomposition ends, the return of NULL (the Mongolian) appears $s=false The loop statement is not true, but if you use ===true, you also need to satisfy: 1. The value equals 2. Type Equals, Then the first decomposed string is not a Boolean value, so the loop stops directly, but if you encounter ' 0 ' when using ' = = ', (not equal to true)!=ture, then the loop stops at this point.
Use! ==false means that the value is not equal, the type is not equal, and only false is returned to stop
Tried, seemingly right, but such a complex logic light would have to be 5 minutes, brain chaos .....

0 !== false  // true0 === true // false

The failure returns false because most of the calls

Successful return of resource type does not successfully return false

Perhaps only when the failure returns false, success does not return a value.

If an interface returns the resource type, how do you compare it with true?

This is related to the return value Ah, if the function return value is true you can do so ah. However, in the case of PHP built-in functions, it is generally wrong to return flase, so use false to determine if there is an error.

  • Related Article

    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.