PHP is_numeric() 函数

PHP 教程 · 2019-01-31 19:12:59

is_numeric() 函数用于检测变量是否为数字或数字字符串。

PHP 版本要求:PHP 4, PHP 5, PHP 7

语法

bool is_numeric ( mixed $var )

参数说明:

  • $var:要检测的变量。

返回值

如果指定的变量是数字和数字字符串则返回 TRUE,否则返回 FALSE。

实例

实例

<?php $var_name1=678; $var_name2="a678"; $var_name3="678"; $var_name4="codercto.com"; $var_name5=698.99; $var_name6=array("a1","a2"); $var_name7=+125689.66; if (is_numeric($var_name1)) { echo "$var_name1 是数字" . PHP_EOL; } else { echo "$var_name1 不是数字" . PHP_EOL ; } if (is_numeric($var_name2)) { echo "$var_name2 是数字" . PHP_EOL ; } else { echo "$var_name2 不是数字" . PHP_EOL ; } $result=is_numeric($var_name3); echo "[ $var_name3 是数字吗? ]" .var_dump($result) . PHP_EOL; $result=is_numeric($var_name4); echo "[ $var_name4 是数字吗? ]" .var_dump($result) . PHP_EOL; $result=is_numeric($var_name5); echo "[ $var_name5 是数字吗? ]" .var_dump($result) . PHP_EOL; $result=is_numeric($var_name6); echo "[ $var_name6 是数字吗? ]" .var_dump($result) . PHP_EOL; $result=is_numeric($var_name7); echo "[ $var_name7 是数字吗? ]" .var_dump($result); ?>

输出结果为:

678 是数字
a678 不是数字
bool(true)
[ 678 是数字吗? ]
bool(false)
[ codercto.com 是数字吗? ]
bool(true)
[ 698.99 是数字吗? ]
bool(false)
[ Array 是数字吗? ]
bool(true)
[ 125689.66 是数字吗? ]

点击查看所有 PHP 教程 文章: https://www.codercto.com/courses/l/5.html

查看所有标签

Algorithms of the Intelligent Web

Algorithms of the Intelligent Web

Haralambos Marmanis、Dmitry Babenko / Manning Publications / 2009-7-8 / GBP 28.99

Web 2.0 applications provide a rich user experience, but the parts you can't see are just as important-and impressive. They use powerful techniques to process information intelligently and offer featu......一起来看看 《Algorithms of the Intelligent Web》 这本书的介绍吧!

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码