百科狗-知识改变命运!
--

is_numeric() - php 变量处理函数

是丫丫呀1年前 (2023-11-21)阅读数 23#技术干货
文章标签数字

is_numeric()

(PHP 4, PHP 5, PHP 7)

is_numeric() - php 变量处理函数

检测变量是否为数字或数字字符串

描述

is_numeric(mixed $var): bool

如果$var是数字和数字字符串则返回TRUE,否则返回FALSE

参见is_bool()、is_float()、is_int()、is_string()、is_object()、is_array()和is_integer()。

If you want the numerical value of a string, this will return a float or int value:

Example: 
Note that the function accepts extremely big numbers and correctly evaluates them.
For example:

The above script will output:
bool(true)
So this function is not intimidated by super-big numbers. I hope this helps someone.
PS: Also note that if you write is_numeric (45thg), this will generate a parse error (since the parameter is not enclosed between apostrophes or double quotes). Keep this in mind when you use this function.
There is another not documented big difference between PHP 5 and PHP 7:
is_numeric("45 "); // with a space at the end
will return true on PHP 5 and false on PHP 7
The documentation does not clarify what happens if you the input is an empty string - it correctly returns false in my experience. Useful to state these odd cases, for when you see code that checks for an empty string and is_numeric, you can tell it's a waste of a comparison.
for strings, it return true only if float number has a dot
is_numeric( '42.1' )//true
is_numeric( '42,1' )//false
The documentation is not completely precise here. is_numeric will also return true if the number begins with a decimal point and/or a space, provided a number follows (rather than a letter or punctuation). So, it doesn't necessarily have to start with a digit.
is_numeric fails on the hex values greater than LONG_MAX, so having a large hex value parsed through is_numeric would result in FALSE being returned even though the value is a valid hex number
I think that is best check solution if u want to create real calculator for example :) 
If you want detect integer of float values, which presents as pure int or float, and presents as string values, use this functions:

Output:
11111111111111111:string is Integer? - no
11111111111111111:string is Float? - yes
1,1111111111111E+16:double is Integer? - no
1,1111111111111E+16:double is Float? - yes
1:integer is Integer? - yes
1:integer is Float? - no
10:string is Integer? - yes
10:string is Float? - no
+1:string is Integer? - yes
+1:string is Float? - no
1.1:string is Integer? - no
1.1:string is Float? - yes
1,1:double is Integer? - no
1,1:double is Float? - yes
0,2:double is Integer? - no
0,2:double is Float? - yes
2:double is Integer? - no
2:double is Float? - yes
.2:string is Integer? - no
.2:string is Float? - yes
2.:string is Integer? - no
2.:string is Float? - yes
-2.:string is Integer? - no
-2.:string is Float? - yes
-.2:string is Integer? - no
-.2:string is Float? - yes
:NULL is Integer? - no
:NULL is Float? - no
Array: array is Integer? - no
Array: array is Float? - no
1:boolean is Integer? - no
1:boolean is Float? - no
:boolean is Integer? - no
:boolean is Float? - no
string:string is Integer? - no
string:string is Float? - no
Referring to previous post "Be aware if you use is_numeric() or is_float() after using set_locale(LC_ALL,'lang') or set_locale(LC_NUMERIC,'lang')":
This is totally wrong!
This was the example code:
-----
 set_locale(LC_NUMERIC,'fr');
 is_numeric(12.25); // Return False
 is_numeric(12,25); // Return True
 is_float(12.25); //Return False
 is_float(12,25); //Return True
-----
This is nonsense!
- set_locale() does not exist, you must use setlocale() instead
- you have to enclose 12,25 with quotes; otherwise PHP will think that 
the function gets _two_ arguments: 12 and 25 (depending on PHP version and setup you may additionally get a PHP warning)
- if you don't enclose 12,25 with quotes the first argument will be the inspected value (12), the second value (25) is discarded. And is_numeric(12) and is_float(12) is always TRUE
Corrected Example:
----
 setlocale(LC_NUMERIC,'fr');
 is_numeric(12.25); // Return True
 is_numeric("12,25"); // Return False
 is_float(12.25); //Return True
 is_float("12,25"); //Return False
----
Remarks:
- is_float(12.25) is _always_ TRUE, 12.25 is a PHP language construct (a "value") and the way PHP interpretes files is definitely _not_ affected by the locale
- is_float("12,25") is _always_ FALSE, since is_float (other than is_numeric): if the argument is a string then is_float() always returns FALSE since it does a strict check for floats
And the corrected example shows: you get the _same_ results for every possible locale, is_numeric() does not depend on the locale.
Sometimes, we need to have no letters in the number and is_numeric does not quit the job.
You can try it this ways to make sure of the number format:
  function new_is_unsigned_float($val) {
    $val=str_replace(" ","",trim($val));
    return eregi("^([0-9])+([\.|,]([0-9])*)?$",$val);
  }
  function new_is_unsigned_integer($val) {
    $val=str_replace(" ","",trim($val));
    return eregi("^([0-9])+$",$val);
  }
  function new_is_signed_float($val) {
    $val=str_replace(" ","",trim($val));
    return eregi("^-?([0-9])+([\.|,]([0-9])*)?$",$val);
  }
  function new_is_signed_integer($val) {
    $val=str_replace(" ","",trim($val));
    return eregi("^-?([0-9])+$",$val);
  }
It returns 1 if okay and returns nothing "" if it's bad number formating.
I needed a number_suffix function that takes numbers with thousand seperators (using number_format() function). Note that this doesn't properly handle decimals.
Example:
 returns: 1,021st
Also, increasing the range above the condition statements increases efficiency. That's almost 20% of the numbers between 0 and 100 that get to end early. 
Two simple functions using is_numeric:

And here is the result:
1: odd? TRUE
0: odd? FALSE
6: odd? FALSE
"italy": odd? FALSE
null: odd? FALSE
1: even? FALSE
0: even? TRUE
6: even? TRUE
"italy": even? FALSE
null: even? FALSE
I find it a little weird that people are having issues with ordinal numbers, it's pretty easy..
Notes are in the commenting, check out the example outputs.

鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com

免责声明:我们致力于保护作者版权,注重分享,当前被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理!邮箱:344225443@qq.com)

图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!

内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)