Comparison of weak php types

Source: Internet
Author: User
Var_dump (0) int (0) var_dump (& #039; 0 & #039;) string (1) & quot; 0 & quot; var_dump (& #039; ssddsd & #039;) string (7) & quot; sasadad & quot; var_dump (0 & #039; sdfsdf & #039;) bool (true) var_dump (& #039; 0 & #039; & #039; sdfsf & #039;) bool (false) in the above comparison, why var_dump (0) // int (0)
Var_dump ('0') // string (1) "0"
Var_dump ('ssddsd') // string (7) "sasadad"
Var_dump (0 = 'sdfsdf ') // bool (true)
Var_dump ('0' = 'sdfsf ') // bool (false)

In the above comparison, why var_dump (0 = 'sdfsdf ') // bool (true) and var_dump ('0' = 'sdfsf') // bool (false)
I think 0 is an integer, and it should be false compared with the aggressive 'sdfsf ....

Why is the actual result the opposite ???

Reply content:

Var_dump (0) // int (0)
Var_dump ('0') // string (1) "0"
Var_dump ('ssddsd') // string (7) "sasadad"
Var_dump (0 = 'sdfsdf ') // bool (true)
Var_dump ('0' = 'sdfsf ') // bool (false)

In the above comparison, why var_dump (0 = 'sdfsdf ') // bool (true) and var_dump ('0' = 'sdfsf') // bool (false)
I think 0 is an integer, and it should be false compared with the aggressive 'sdfsf ....

Why is the actual result the opposite ???

var_dump(0=='sdfsdf')

When processing this sentence, comparing different types of 'sdfsdf 'will be forcibly converted to the int type, that is, int 0, so the result is true.

var_dump('0'=='sdfsf') 

When this sentence is processed, the content of the same type is compared directly and the content is different. Therefore, the result is false.

You can try var_dump (int) ("sdfsf"); the result is int 0.

Https://segmentfault.com/q/1010000000095573/a-1020000000098176

Why is it wrong... Do you need to explain this... The primary factor for determining equality is that the content must be equal,==Non-strict mode will automatically convert the two types to the same one. The string content is different. Why do you think it is equal...

You are wrong.
The direct conversion of the comparison operator is: Compare the integer with the string, convert the string to an integer, then the string sdfsdf is converted to an integer 0, so the third is true.
The fourth is true. Because they are both strings, they are compared directly. Therefore, the string 0 and sdfsf are naturally not equal to false.

==Comparison only compares values. Different types are converted to the same type comparison.

Use===, The value and type must be the same to true.

These official documents are all written in. Please read them carefully.

var_dump(0=='sdfsdf');For a weak type, data type is converted to the same type before the value is equal.
The comparison rule between strings and values is to convert strings into numbers. Therefore, the above comparison can be seen

var_dump(0 == (int)'sdfsdf');

var_dump('0'=='sdfsf');Both are strings, that is, whether the comparison values are equal.

The string and number are converted to 0, which also exists in the database.

Http://php.net/manual/zh/language.operators.comparison.php
If you compare a number with a string or a string that involves the number content,
The string is converted to a value and compared to a value. This rule applies to switch statements.
When = or! = During comparison, no type conversion is performed because the types and values must be compared.

When comparing strings with numbers in PHP, strings are converted to numbers for comparison. The conversion process is similar to intval ():

Echo intval ('5stars'); Output 5print_r ('5stars' = 5); Output 1var_dump ('5stars' = 5); Output bool (true) var_dump ('5stars' = 5); Output bool (false) var_dump ('0e1 '= '0e2'); Output bool (true) var_dump ('0e1 '= '0e2'); Output bool (false)

Comparison strings in PHP should be usedConstant =Or strcmp.

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.