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
广告
© . All rights reserved.