English 中文(简体)
PHP Comparison Operators and Data Types
原标题:

I m currently working through O Reilly s "Programming PHP" and have come across this table titled "Type of comparison performed by the comparison operators":

First Operand              | Second Operand             | Comparison
-----------------------------------------------------------------------
Number                     | Number                     | Numeric
String that is numeric     | String that is numeric     | Numeric
String that is numeric     | Number                     | Numeric
String that is not numeric | Number                     | Lexicographic
String that is numeric     | String that is not numeric | Lexicographic
String that is not numeric | String that is not numeric | Lexicographic

My rule of thumb for which type of comparison is performed has been "numeric if and only if at least one operand is a number or both operands are numeric strings". This seems to be supported by the php.net page on Comparison Operators, which states "If you compare an integer with a string, the string is converted to a number. If you compare two numerical strings, they are compared as integers."

However, this would imply that the comparison in the fourth row of the table should be "Numeric". Does the table contain an error, or is my rule wrong?

最佳回答

Editted: A complete about face based on the comments.

Your summary is correct, and the table is wrong. A conversion is attempted on the numeric bit at the start of the string if one operand is numeric. The conversion returns zero if there is no numeric leader. The conversion takes place for decimals and the rational results of calculations, not just integers.

The code below demonstrates the behaviours


if (2 >  10 little pigs )
        print  Integer does not coerce string ."
";
else
        print  Integer does coerce string ."
";

if (2.5 >  10 little pigs )
        print  Decimal does not coerce string ."
";
else
        print  Decimal does coerce string ."
";

if (5/3 >  2 little pigs )
        print  Rational result does not coerce string ."
";
else
        print  Rational result does coerce string ."
";

if (0 ==  No little pigs )
        print  Non numeric coerced to zero ."
";
else
        print  Non numeric not coerced ."
";

if (-0.156 >  -127 is a minumum value of a signed 8 bit number )
        print  Negative decimal does coerce string ."
";
else
        print  Negative decimal does not coerce string ."
";

if ( -0.156  >  -127 )
        print  Both are converted if all numeric ."
";
else
        print  No conversion if both are all numeric ."
";

if ( -0.156  >  -127 is a minumum value of a signed 8 bit number )
        print  Successful conversion of one does coerce the other ."
";
else
        print  Successful conversion of one does not coerce the other ."
";

问题回答

暂无回答




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签