Php comparison operator bug_PHP tutorial

Source: Internet
Author: User
Tags php sample code
Php comparison operator bug. The first choice is to give a php sample code. if you fully understand why the following results appear, you do not need to waste any time on this blog. here is a question for beginners of php.
First, give a php sample code. if you fully understand why the following results appear, you do not need to waste time on this blog, this is to let php beginners pay attention to possible mistakes.


[Php]

Var_dump ("abcdefg" = "0"); // bool (false)
Var_dump ("abdsafd" = 0); // bool (true)

Var_dump ("abcdefg" = "0"); // bool (false)
Var_dump ("abdsafd" = 0); // bool (true)
If you are not too clear about the above results, follow the blog to continue learning!


Comparison Operators
Comparison operators, as their names imply, allow comparison of two values.


Comparison operator example name result
$ A = $ B = TRUE, if $ a is equal to $ B
$ A ===$ B all equal to TRUE, if $ a is equal to $ B, and their types are the same
$! = $ B is not equal to TRUE. if $ a is not equal to $ B
$ A <> $ B is not equal to TRUE. if $ a is not equal to $ B
$! = $ B is not fully equal to TRUE. if $ a is not equal to $ B, or they are of different types
$ A <$ B is less than TRUE, if $ a is strictly less than $ B
$ A> $ B is greater than TRUE. if $ a is strictly greater than $ B
$ A <= $ B is less than or equal to TRUE. if $ a is less than or equal to $ B
$ A >=$ B is greater than or equal to TRUE. if $ a is greater than or equal to $ B

Note:
If you compare an integer with a string, the string is converted to an integer.
Compare two numeric strings as integers


Here, let's talk about the above php code. var_dump ("abcdefg" = "0") is false because it is a comparison of two strings and corresponds to the strcmp function of c, therefore, it should be false. However, if var_dump ("abdsafd" = 0) is true, you need to learn the rules for converting strings to integers.


String to integer
When a string is used in a numeric environment, the result and type are as follows:


If the string does not contain '. ', 'e', or 'e', and the numeric value conforms to the limitation of the integer type (defined by PHP_INT_MAX). This string can be considered as an integer, in other cases, it is regarded as a float.


The start part of the string is given its value. if the string starts with a valid number, this number can be directly used. Otherwise, the value is 0. A valid value is a symbol followed by one or more digits (which may have a decimal point), followed by an optional index symbol, such as 'E' or 'e ', followed by one or more numbers.


Sample code:


[Php]

$ Foo = 1 + "10.5"; // $ foo is float (11.5)
$ Foo = 1 + "bob-1.3e3"; // $ foo is integer (1)
$ Foo = 1 + "bob3"; // $ foo is integer (1)
$ Foo = 1 + "10 Small Pigs"; // $ foo is integer (11)
$ Foo = 4 + "10.2 Little Piggies"; // $ foo is float (14.2)
$ Foo = "10.0 pigs" + 1; // $ foo is float (11)
$ Foo = "10.0 pigs" + 1.0; // $ foo is float (11)
?>

$ Foo = 1 + "10.5"; // $ foo is float (11.5)
$ Foo = 1 + "bob-1.3e3"; // $ foo is integer (1)
$ Foo = 1 + "bob3"; // $ foo is integer (1)
$ Foo = 1 + "10 Small Pigs"; // $ foo is integer (11)
$ Foo = 4 + "10.2 Little Piggies"; // $ foo is float (14.2)
$ Foo = "10.0 pigs" + 1; // $ foo is float (11)
$ Foo = "10.0 pigs" + 1.0; // $ foo is float (11)
?>
Here we can thoroughly explain that var_dump ("abcdefg" = 0) is true, because the comparison operator first forcibly converts "abcdefg" to an integer 0, because 0 = 0 is true

The first choice is to give a php sample code. if you fully understand why the following results appear, you don't need to waste any time on this blog. here is to let php beginners pay attention to it...

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.