is_numeric関数は、数値形式の文字列かどうかを判定する関数です。
その基準について、ドキュメントには以下のように記載されています。
指定した変数が数値であるかどうかを調べます。数値形式の文字列は以下の要素から なります。(オプションの)符号、任意の数の数字、(オプションの)小数部、 そして(オプションの)指数部。つまり、+0123.45e6 は数値として有効な値です。十六進表記(0xf4c3b00c など) や二進表記 (0b10100111001 など)、そして八進表記 (0777 など) は認められません。
<thead> <tr> <th>バージョン</th> <th>説明</th> </tr> </thead> <tbody> <tr> <td>7.0.0</td> <td> 十六進表記(<em>0xf4c3b00c</em> など) の文字列は数値形式の文字列とみなされなくなりました。 つまり、<strong>is_numeric()</strong> は <strong><code>FALSE</code></strong> を返すようになりました。 </td> </tr> </tbody>
これに従うならば、以下のコードは
<?php
/**
* 出力をテキストとして表示するため、テキストファイルのContent-Typeを送信
*/
header('Content-Type: text/plain');
$tests = [
'42', // 普通の数値
'0xf4c3b00c', // 十六進表記
'0777', // 八進表記
'0b10100111001', // 二進表記
'1337e0', // 指数部を含む数値
'+42', // 符号付きの数値
'3.141592', // 小数部を含む数値
'not numeric', // 数値ではない文字列
];
foreach ($tests as $element) {
echo '"', $element, '" is' . (is_numeric($element) ? '' : ' NOT') . ' numeric', PHP_EOL;
}
このような出力となるはずです。
PHP 7以上
"42" is numeric
"0xf4c3b00c" is NOT numeric
"0777" is NOT numeric
"0b10100111001" is NOT numeric
"1337e0" is numeric
"+42" is numeric
"3.141592" is numeric
"not numeric" is NOT numeric
PHP 7未満
"42" is numeric
"0xf4c3b00c" is numeric
"0777" is NOT numeric
"0b10100111001" is NOT numeric
"1337e0" is numeric
"+42" is numeric
"3.141592" is numeric
"not numeric" is NOT numeric
しかし、実際に実行してみると…
PHP 7.0.0beta2
"42" is numeric
"0xf4c3b00c" is NOT numeric
"0777" is numeric
"0b10100111001" is NOT numeric
"1337e0" is numeric
"+42" is numeric
"3.141592" is numeric
"not numeric" is NOT numeric
ドキュメントに従った動作との差分
"42" is numeric
"0xf4c3b00c" is NOT numeric
-"0777" is NOT numeric
+"0777" is numeric
"0b10100111001" is NOT numeric
"1337e0" is numeric
"+42" is numeric
"3.141592" is numeric
"not numeric" is NOT numeric
PHP 7.0.0alpha2
"42" is numeric
"0xf4c3b00c" is NOT numeric
"0777" is numeric
"0b10100111001" is NOT numeric
"1337e0" is numeric
"+42" is numeric
"3.141592" is numeric
"not numeric" is NOT numeric
ドキュメントに従った動作との差分
"42" is numeric
"0xf4c3b00c" is NOT numeric
-"0777" is NOT numeric
+"0777" is numeric
"0b10100111001" is NOT numeric
"1337e0" is numeric
"+42" is numeric
"3.141592" is numeric
"not numeric" is NOT numeric
PHP 5.6.8
"42" is numeric
"0xf4c3b00c" is numeric
"0777" is numeric
"0b10100111001" is NOT numeric
"1337e0" is numeric
"+42" is numeric
"3.141592" is numeric
"not numeric" is NOT numeric
ドキュメントに従った動作との差分
"42" is numeric
"0xf4c3b00c" is numeric
-"0777" is NOT numeric
+"0777" is numeric
"0b10100111001" is NOT numeric
"1337e0" is numeric
"+42" is numeric
"3.141592" is numeric
"not numeric" is NOT numeric
PHP 5.6.10
"42" is numeric
"0xf4c3b00c" is numeric
"0777" is numeric
"0b10100111001" is NOT numeric
"1337e0" is numeric
"+42" is numeric
"3.141592" is numeric
"not numeric" is NOT numeric
ドキュメントに従った動作との差分
"42" is numeric
"0xf4c3b00c" is numeric
-"0777" is NOT numeric
+"0777" is numeric
"0b10100111001" is NOT numeric
"1337e0" is numeric
"+42" is numeric
"3.141592" is numeric
"not numeric" is NOT numeric
PHP 5.5.24
"42" is numeric
"0xf4c3b00c" is numeric
"0777" is numeric
"0b10100111001" is NOT numeric
"1337e0" is numeric
"+42" is numeric
"3.141592" is numeric
"not numeric" is NOT numeric
ドキュメントに従った動作との差分
"42" is numeric
"0xf4c3b00c" is numeric
-"0777" is NOT numeric
+"0777" is numeric
"0b10100111001" is NOT numeric
"1337e0" is numeric
"+42" is numeric
"3.141592" is numeric
"not numeric" is NOT numeric
PHP 5.4.42
"42" is numeric
"0xf4c3b00c" is numeric
"0777" is numeric
"0b10100111001" is NOT numeric
"1337e0" is numeric
"+42" is numeric
"3.141592" is numeric
"not numeric" is NOT numeric
ドキュメントに従った動作との差分
"42" is numeric
"0xf4c3b00c" is numeric
-"0777" is NOT numeric
+"0777" is numeric
"0b10100111001" is NOT numeric
"1337e0" is numeric
"+42" is numeric
"3.141592" is numeric
"not numeric" is NOT numeric
PHP 5.4.40
"42" is numeric
"0xf4c3b00c" is numeric
"0777" is numeric
"0b10100111001" is NOT numeric
"1337e0" is numeric
"+42" is numeric
"3.141592" is numeric
"not numeric" is NOT numeric
ドキュメントに従った動作との差分
"42" is numeric
"0xf4c3b00c" is numeric
-"0777" is NOT numeric
+"0777" is numeric
"0b10100111001" is NOT numeric
"1337e0" is numeric
"+42" is numeric
"3.141592" is numeric
"not numeric" is NOT numeric
ドキュメントでは、八進表記(0777
など)は認められないと明記されています。
にも関わらず、すべてのバージョンのPHPにおいて、明らかに八進表記でtrueが返されています。