PHP 5.4.4ãã==ã®æåãä¸æ®µã¨é£ãããªãã¾ãã
PHPã®==ã¯ä¸¡è¾ºãé©å½ã«åãã£ã¹ããã¦ããæ¯è¼ãããããªæ¼ç®åã§ãããã®åãã£ã¹ãã®è¦åã¯é£è§£ãããä¸ã«ããã¥ã¡ã³ããä¸ååãªãããPHPããã°ã©ãã§ãå®ç§ã«ç解ãã¦ãã人ã¯ã»ã¨ãã©å± ãªããããã®å°è±¡ã§ãããã°ã®åå ã«ãªããããªãããããªãã¹ã==ã使ããªãããã«ãã¦ããPHPããã°ã©ããå¤ãã¯ãã§ãã
ã¨ããã§ããã®==æ¼ç®åã®æåãPHP 5.4.4ããå¤æ´ããã¦ãããã¨ã¯ãã¾ãç¥ããã¦ãã¾ãããæ¬ç¨¿ã§ã¯ãã®å 容ãç´¹ä»ãã¾ãã
Bug #54547 ã®é¨å
ã¾ãã¯ãã®ä»æ§å¤æ´ã®çµç·¯ãç´¹ä»ãã¾ãã
2å¹´ã»ã©æãHacker Newsã§2^63付近の整数に対応する文字列をPHPで比較したときの挙動がおかしいというスレッドãçãä¸ãã£ããã¨ãããã¾ãããå ·ä½çã«ã¯ãPHPã§ã¯ã'9223372036854775807' == '9223372036854775808'ããtrueã«ãªãã¨ãã話é¡ã§ãPHPã®ä»æ§ãDISããããªæµãã§ããã
åã®ããã«å¤ãããã®PHPã¦ã¼ã¶ã¼ã«ã¨ã£ã¦ã¯==ã®æåããã¢ããã¨ã¯å¸¸èã§ããããã®ä¾ã§æµ®åå°æ°ç¹æ°æ¯è¼ããã¦ããã®ãä¸ç®çç¶ãä»ã®è¨èªã®äººãå©ãã«æ¥ãã®ãæ¥æ¬ã§ã¯ãããããã¨ã§ãæµ·å¤ãå¨åé ãã§åãæµãã«ãªã£ã¦ãã®ããªã¼ã¨ãããããã®å°è±¡ã§ãããåããããã¨ããã¯ä»æ§ã§ããã==ã®å®è£ ã¯è¤éããã¦æ´åæ§ãä¿ã¡ã¤ã¤ä¸æºã解æ¶ãããããªä»æ§å¤æ´ã¯ä¸å¯è½ã ã¨æãã¦ããããããããå©ãããããä¿®æ£ã¯ããããªãã¨æã£ã¦ãã¾ããã
ã¨ãããPHPã®ä¸ã®äººãã ã·ã£ã¯ã·ã£ãã¦ããã®ãä½ãªã®ãã¯ãããã¾ããããããããã°ã¨ãã¦ä¿®æ£ããæµãã«ãªãã¾ããããã®ãã°å ±åã¨ä¿®æ£ã¾ã§ã®æµãã¯ãPHP :: Bug #54547 :: wrong equality of string numbersãã§ç¢ºèªãããã¨ãã§ãã¾ãããããPHP 5.4.4ããåãè¾¼ã¾ãã¦ãããä»åç´¹ä»ããå 容ã¨ãããã¨ã«ãªãã¾ãã
PHP 5.4.4ã§ã®å¤æ´å 容
PHP 5.4.4ããã¯ãä¸ã§ç´¹ä»ãã'9223372036854775807' == '9223372036854775808'ããfalseãè¿ãããã«ãªã£ã¦ãã¾ãã
ãã®å¤æ´ã®è©³ç´°ã«ã¤ãã¦ããã¥ã¢ã«ãªã©ã«ã¯è¨è¿°ãè¦å½ããã¾ããããã½ã¼ã¹ããã±ã¼ã¸ã®./UPGRADINGã«æ¬¡ã®ããã«æ¸ãã¦ããã¾ãã
Long numeric strings that do not fit in integer or double (such as
"92233720368547758070") are compared using string comparison if
they could otherwise result in precision loss - since 5.4.4.
翻訳ããã¨æ¬¡ã®ããã«ãªãã¾ãã
æ´æ°ãæµ®åå°æ°ç¹æ°ã«ããã¾ããªããããªå·¨å¤§ãªæ°å¤æååï¼"92233720368547758070"ãªã©ï¼ãæ¯è¼ããå ´åã«ã
æååæ¯è¼ããªãã¨ç²¾åº¦ãè½ã¡ããããªå ´åã«ã¯æååæ¯è¼ã使ããã¾ããï¼PHP5.4.4ããï¼
ããããè¨è足ãããªã®ã§ãããããã¯æ°å¤æååå士ãæ¯è¼ããå ´åã®è©±é¡ã§ããPHP 5.4.3以åã§ã¯æ°å¤æååå士ãæ¯è¼ããå ´åã¯ããªããæ´æ°ã¾ãã¯æµ®åå°æ°ç¹æ°ã«ãã£ã¹ãããã¦ããæ¯è¼ããã¦ãã¾ããããããããã£ã¹ãã§ç²¾åº¦ãè½ã¡ããããªæ°å¤æååå士ã®å ´åã¯æååæ¯è¼ããããã¨ããã®ãPHP 5.4.4ã§ã®å¤æ´ã¨ãããã¨ã«ãªãã¾ãã
説æã®ããã64bitç°å¢ã®PHP 5.5.11ã§ã®å®è¡ä¾ã示ãã¾ãã
<?php $x1 = "9223372036854775807"; $y1 = "0X7FFFFFFFFFFFFFFF"; var_dump($x1==$y1); // bool(true), æ°å¤æ¯è¼ãã¦ããï¼æååæ¯è¼ãã¦ãããfalseã®ã¯ãï¼ $x2 = "9223372036854775808"; $y2 = "0X8000000000000000"; var_dump($x2==$y2); // bool(false), æååæ¯è¼ãã¦ããï¼æ°å¤æ¯è¼ãã¦ãããtrueã®ã¯ã, PHP 5.4.3以åã§ã¯trueï¼
ãã®ä¾ã®$x1,$y1ã¯æ´æ°ã®ä¸éå¤ã¨ãªããããªæ°å¤æååã§ããããã¯æ´æ°ã¨ãã¦ããã¿ãªè¡¨ç¾ã§ããã®ã§ãæ°å¤æ¯è¼ããã¾ããããã¯PHP 5.4.3以åã¨åãæåã§ãã
ä¸æ¹ã$x2ã$y2ã¯æ´æ°ã®ä¸éå¤ãè¶ ãã¦ãããæ°å¤æ¯è¼ããã¨ãããæµ®åå°æ°ç¹æ°ã«ãã£ã¹ããã¦æ¯è¼ãããã¨ã«ãªãã¾ããå®éãPHP 5.4.3ã¾ã§ã¯æµ®åå°æ°ç¹æ°ã¨ãã¦æ°å¤æ¯è¼ããã¦ãã¾ããããããããããã®æ´æ°ã¯å¤§ãããããããæµ®åå°æ°ç¹æ°ã«ãã£ã¹ãããã¨ç²¾åº¦ãè½ã¡ã¦ãã¾ãæ¯è¼æ¼ç®ãä¸æ£ç¢ºã«ãªãããããããã¾ãããã®ãããªå ´åã«PHP 5.4.4ã§ã¯æååæ¯è¼ããããã«ãªã£ãã¨ããããã§ãã
æ´æ°ã«å¯¾å¿ããæ°å¤æååã®å ´åãæµ®åå°æ°ç¹æ°ã«ãã£ã¹ããã¦ç²¾åº¦ãè½ã¡ãã¨å¤æãããã®ã¯-2^63-1以ä¸ã¾ãã¯2^63以ä¸ï¼æ´æ°ã®ç¯å²å¤ãã©ããã64bitç°å¢ã®å ´åï¼ã®ã¨ãããããã¯-2^53以ä¸ã¾ãã¯2^53以ä¸ï¼æµ®åå°æ°ç¹æ°ã®ä»®æ°é¨ã§è¡¨ç¾ã§ãããã©ããã32bitç°å¢ã®å ´åï¼ã®ã¨ãã«ãªãã¾ãã
ä»æ§å¤æ´ã§ã®èãæ¼ã
ã¨ããã§ããã®ä»æ§å¤æ´ã§ã¯æµ®åå°æ°ç¹æ°ã«å¯¾å¿ããæ°å¤æååã«ã¤ãã¦ä»æ§ä¸ã®èãæ¼ããããããã«æãã¾ããã¤ã¾ããæµ®åå°æ°ç¹æ°ã®å ´åã«ã¯ãæååæ¯è¼ããªãã¨ç²¾åº¦ãè½ã¡ããã¨ã¯ã©ããããã¨ããèªæã§ã¯ããã¾ããã
PHPã®ã½ã¼ã¹ã³ã¼ãã確èªãã¦ã¿ãã¨ããã64bitç°å¢ã§ã¯10^19以ä¸ã®æµ®åå°æ°ç¹æ°ã«å¯¾å¿ããæ°å¤æååå士ãæµ®åå°æ°ç¹æ°ã«ãã£ã¹ããã¦å·®ã0ã ã£ãå ´åã¯æååæ¯è¼ããã¨ããå®è£ ã«ãªã£ã¦ãã¾ããã10^19ã¨ããã®ã¯æµ®åå°æ°ç¹æ°ã«ã¨ã£ã¦æå³ã®ããå¢çå¤ã§ã¯ç¡ããããæ·±ãèããããããã«ã¯æãã¾ãããããããå½ã¦ã人ã¯ãããªå¢çå¤ã«ãªã£ã¦ãããã¨ããæ°ã¥ãã¦ããªãå¯è½æ§ãããã¨æãã¾ãã
ä¸è¨ã®å 容ã¯ãã½ã¼ã¹ã³ã¼ãZend/zend_operators.cã®zendi_smart_strcmp()ã¨Zend/zend_operators.hã®is_numeric_string_ex()ã追ãã°ç¢ºèªã§ãã¾ãã
æµ®åå°æ°ç¹æ°ã«å¯¾å¿ããæ°å¤æååã®æ¯è¼ã«ã¤ãã¦ãå®è¡ä¾ã示ãã¾ãã
<?php $x3 = "9999999999999999999.0"; $y3 = "9999999999999999999.1"; var_dump($x3==$y3); // bool(true), æ°å¤æ¯è¼ãã¦ããï¼æååæ¯è¼ãã¦ãããfalseã®ã¯ãï¼ $x4 = "10000000000000000000.0"; $y4 = "10000000000000000000.00"; var_dump($x4==$y4); // bool(false), æååæ¯è¼ãã¦ããï¼æ°å¤æ¯è¼ãã¦ãããtrueã®ã¯ã, PHP 5.4.3以åã§ã¯trueï¼
ã¡ãªã¿ã«ã$x3ã$y3ã$x4ã$y4ãããããæµ®åå°æ°ç¹æ°ã«ãã£ã¹ãããã¨å ¨ã¦çããæ°ã«ãªãã¾ããã«ãããããããæ°å¤æååå士ã®æ¯è¼ã§ã¯10é²è¡¨è¨ã®æ¡æ°ã«ãã£ã¦æåãå¤ããã¨ãããã¨ã§ãã
ã¾ã¨ã
- PHPã§==ã使ã£ã¦æ°å¤æååå士ãæ¯è¼ããå ´åãååã¨ãã¦æ°å¤æ¯è¼ããã¾ã
- PHP5.4.4以éã§ã¯ãæ¯è¼ããæ°å¤æååå士ã大ããã¦ä¸¡è
ãè¿ãããå ´åã«ã¯æååæ¯è¼ã«ãªãã¾ãã
- æ´æ°ã§ããã°-2^63-1以ä¸ã¾ãã¯2^63以ä¸ï¼64bitç°å¢ï¼ã®å ´å
- æµ®åå°æ°ç¹æ°ã§ããã°-10^19以ä¸ã¾ãã¯10^19以ä¸ï¼64bitç°å¢ï¼ã®å ´å
- æ¯è¼æ¼ç®åãå½±é¿ãåããã®ã§ãã½ã¼ãå¦çãªã©ã«ã注æãå¿ è¦ã§ã
ä»ã¾ã§ç¹ã«æ²é³´ãèããã¦ãã¦ãã¾ããããããªã大ããæ°ã«ããå½±é¿ããªãããããã®å¤æ´ã«ããå®å®³ã¯ç¡ãã ããã¨èãã¦ãã¾ããã¨ã¯ããããããªãã¢ãä¿®æ£ã«ã¤ãã¦2å¹´é誰ãé¨ãã§ããªãã£ãã®ã¯ã©ããªã®ããã¨ãæãã¾ãããçããå°ãæ¬ä½ã®ã½ã¼ã¹ã³ã¼ããèªã¿ã¾ãããããããè¨ãåã2å¹´éèªãã§ãªãã£ãããã§ããã©ãâ¦ã