Ask a question: why php is often used! = False indicates true. Why not use = true directly?

Source: Internet
Author: User
Tags strtok
For example, the following code: {code...} while ($ strue) {} cannot be displayed? Where can I use true ~~ For beginners, please take care of O (∩ _ ∩) O ~ Happy New Year ~ My own understanding is :! If 'false' is true, 1. The value is not equal to 2. The type is not equal to. When the strtok decomposition ends... for example, the following code:

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

While ($ s = true) {} Why cannot it be displayed?
Where can I use true ~~
For beginners, please take care of O (∩ _ ∩) O ~
Happy New Year ~

My own understanding is :! = False: 1. the value is not equal to 2. the type is not equal to. When strtok decomposition ends, null (menghaha) is returned. $ s = false makes the loop statement invalid. However, if = true is used, the following conditions must be met: 1. the value is equal to 2. if the type is equal to, the first string to be decomposed is not a Boolean value, so the loop stops directly. However, if '=' is used, when '0' is encountered, (not equal to true )! = True, then the loop will also stop.
Use! = False, it means the value is not equal to, the type is not equal to, and only false is returned to stop
I tried it. It seems right, but it takes five minutes to think about such a complicated logic .....

Reply content:

For example, the following code:

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

While ($ s = true) {} Why cannot it be displayed?
Where can I use true ~~
For beginners, please take care of O (∩ _ ∩) O ~
Happy New Year ~

My own understanding is :! = False: 1. the value is not equal to 2. the type is not equal to. When strtok decomposition ends, null (menghaha) is returned. $ s = false makes the loop statement invalid. However, if = true is used, the following conditions must be met: 1. the value is equal to 2. if the type is equal to, the first string to be decomposed is not a Boolean value, so the loop stops directly. However, if '=' is used, when '0' is encountered, (not equal to true )! = True, then the loop will also stop.
Use! = False, it means the value is not equal to, the type is not equal to, and only false is returned to stop
I tried it. It seems right, but it takes five minutes to think about such a complicated logic .....

PHP is a weak language. The most important reason is implicit type conversion.

Assignment, calculation, and comparison are the main manifestations:
Assignment:

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

In the above example, the type of $ a changes with the type of the value assignment. You must know that this is not possible in C, because the variable types in C are determined when they are declared, they cannot be changed after they are determined.

Computing:

$ A = 1; $ B = '2'; echo $ a + $ B; // The result is 3.

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

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

Python is also a strong type language and does not deduce the type of the variable. Therefore, an error is directly thrown to you. But how does PHP do it?
The connection string in Python is also used.+But PHP needs to use.. In PHP, when you use+The variables on both sides of the symbol are first converted to the numeric type (floating point, integer). Similarly.In fact, both sides of the symbol are converted into strings first. This is because Python does not need to be converted. In the case of digital computing, it can be connected to strings.

This conversion process is also very interesting:

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

As you can see above, you should have guessed: the string is searched from the past to the next until a non-numeric character is encountered.

Next we will encounter the following situation: comparison.

==And===Including!=And!==As you have already guessed, one will compare the type and the other will not compare the type. Used in PHP internal descriptionequalAndidenticalThese two words are used to describe. Feel the difference.

The exact order is:===And!==Is to first determine whether the types are the same, and then compare the specific value. If the types are different, there is no need to continue the comparison. This means that even1.0 === 1What you get will also befalseBecause the types are different.

Actually==You also need to check the type, but the action is: it will first make a judgment based on the variable types on both sides of the operator to perform implicit conversion on the variable and then compare it! Here I won't tell you the order of conversion, but you should know the basics:

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

You may not know, for example:

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

This problem is basically clear here. Because'0' == falseYes, this is to use!==.

Come backstrtokActually, it is a strange function. It is a built-in iteration function. If you use!=Can not be accurately retrieved.strtok('hello world 0', ' ')The third segment of this decomposition0.

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

Some functions return values by default.intOrstringType. It is returned only when a failure occurs.false. Therefore, when making a judgment, use!==false. For examplestrtokIf the execution result is the expected resultstringOtherwisefalse. So ~~

Update

For examplestrposFunctions such$strLi$findWhere, for example123Searching1Will returnint(0). Only when not foundfalse

However,if (0) The result isfalse, The logic in if will not be executed. So Judgeif ($result !== false) Is the correct method.

About!==And!= ,===And==To determine the difference, Google it yourself.

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

Both expressions are true.

My own understanding is :! = False: 1. the value is not equal to 2. the type is not equal to. When strtok decomposition ends, null (menghaha) is returned. $ s = false makes the loop statement invalid. However, if = true is used, the following conditions must be met: 1. the value is equal to 2. if the type is equal to, the first string to be decomposed is not a Boolean value, so the loop stops directly. However, if '=' is used, when '0' is encountered, (not equal to true )! = True, then the loop will also stop.
Use! = False, it means the value is not equal to, the type is not equal to, and only false is returned to stop
I tried it. It seems right, but it takes five minutes to think about such a complicated logic .....

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

Because most calls fail, false is returned.

If the resource type is unsuccessful, false is returned.

Maybe false is returned only when the request fails. If the request succeeds, no value is returned.

If the resource type returned by an interface is true, how can you compare it?

This is related to the return value. If the return value of the function is true, you can do so .. However, for PHP built-in functions, flase is usually returned due to an error. Therefore, false is used to determine whether an error occurs.

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.