2009-11-16 3 views
1
나는 현재 오라일리의 "프로그래밍 PHP"를 통해 일하고 있어요 및 제목이 테이블을 가로 질러 온

"비교 연산자에 의해 수행되는 비교의 유형"의PHP 비교 연산자 및 데이터 유형

 
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 

내 규칙 비교 유형이 수행되는 엄지 손가락은 "하나 이상의 피연산자가 숫자이거나 양쪽 피연산자가 숫자 문자열 인 경우에만 숫자입니다."였습니다. 이것은 php.net page on Comparison Operators에 의해 지원되는 것으로 보입니다. "정수를 문자열과 비교하면 문자열이 숫자로 변환됩니다. 두 개의 숫자 문자열을 비교하면 정수로 비교됩니다."

그러나 이것은 표의 네 번째 행의 비교가 "숫자"이어야 함을 의미합니다. 테이블에 오류가 있거나 규칙이 잘못 되었습니까?

+0

규칙이 잘못된 행동을 보여줍니다. ''string "== 0'과''intval ("string ") == 0'을 비교해보십시오. –

+0

@Ast Derek : 두 비교의 결과는 사실 인 것 같습니다. – user200783

답변

2

편집 됨 : 댓글을 기반으로 한 완전한 얼굴.

요약이 정확하고 테이블이 잘못되었습니다. 하나의 피연산자가 숫자 인 경우 문자열 시작 부분의 숫자 비트에서 변환이 시도됩니다. 수치 지시선이 없으면 변환은 0을 반환합니다. 변환은 정수만이 아니라 계산의 합리적인 결과와 소수에 대해 수행됩니다.

아래 코드는

 

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

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

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

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

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

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

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

php.net 문은 "정수를 문자열과 비교하면 문자열이 숫자로 변환됩니다."라고 말합니다. 이것은 정수와 문자열 (임의의 문자열, 숫자 또는 아닌)을 숫자로 비교한다는 것을 의미하지는 않습니까? 그것은 당신의 "번역"에 동의하지 않습니다. – user200783

+0

php.net 페이지를 다시 읽으면 인용 한 테이블과 페이지 사이의 용어가 일치하지 않는 것으로 보입니다. 모든 숫자가 똑같이 행동하는 것은 아닙니다. 이 사건에 대한 데모 코드를 제안하려고합니다. – Bell