PHP - is_numeric() 函式



定義和用法

is_numeric() 函式驗證變數是否為數字或數字字串。

語法

bool is_numeric ( mixed $value )

引數

序號 引數及描述
1

要評估的變數值

返回值

如果value 是數字或數字字串,則此函式返回 true,否則返回 false

依賴

PHP 4 及以上版本。

示例

以下示例演示了不同型別變數的返回值:

<?php
   $test_variable = array(
      "21",
      1443,
      0x539,
      01341,
      0b10100111001,
      1337e0,
      "0x539",
      "01341",
      "0b10100111001",
      "1337e0",
      "tutorialspoint",
      array(),
      9.1,
      null,
      '',
   );

   foreach ($test_variable as $var) {
      if (is_numeric($var)) {
         echo $var . " is numeric <br>";
      } else {
         echo $var. " is NOT numeric<br>";
      }
   }
?>

輸出

這將產生以下結果:

21 is numeric
1443 is numeric
1337 is numeric
737 is numeric
1337 is numeric
1337 is numeric
0x539 is NOT numeric
01341 is numeric
0b10100111001 is NOT numeric
1337e0 is numeric
tutorialspoint is NOT numeric
Array is NOT numeric
9.1 is numeric
is NOT numeric
is NOT numeric
php_variable_handling_functions.htm
廣告