Markdown

[PHP] Check is Number

檢查金額是否為數字
if is_numeric($input) === true
then either
is_float($input) === true OR
is_int($input) === true OR
分別需要注意
第一種 0x36這種也過
第二種 整數或小數點
第三種 整數
[eric_tu@localhost ~]$ php -a
Interactive shell

php > echo intval(23);
23
php > echo intval(231asad);
PHP Parse error:  syntax error, unexpected 'asad' (T_STRING) in php shell code on line 1
php > echo intval('231asad');
231
php > echo is_int('23');
php > echo intval(0x3123);
12579
php > echo intval('.x2130');
0
php > is_numeric('123ads')
php > echo is_numeric('123ads');
PHP Parse error:  syntax error, unexpected echo (T_ECHO) in php shell code on line 2
php > echo preg_match('/^[0-9]*$/', '21313','asd21')
php > echo preg_match('/^[0-9]*$/', '21313','asd21');
PHP Fatal error:  Only variables can be passed by reference in php shell code on line 1
php > echo preg_match('/^[0-9]*$/', '21313');
1
php > echo preg_match_all('/^[0-9]*$/', '21313');
1
php > echo preg_match_all('/^[0-9]*$/', '21313', '213');
PHP Fatal error:  Only variables can be passed by reference in php shell code on line 1
php > echo preg_match('/^[0-9]*$/', '21313', '213');
PHP Fatal error:  Only variables can be passed by reference in php shell code on line 1
php > echo preg_match('/^[0-9]*$/', '21313');
1
php > echo ctype_digit(213);
php > echo ctype_digit('213');
1
php > echo ctype_digit('213asd');
php > echo ctype_digit(1);          #GG
php > echo ctype_digit(1123);
1
php > echo ctype_digit('1');
1
php > echo ctype_digit("1");
1
php > 
dump(ctype_digit("depositId"),depositId,ctype_digit($depositAmount));~
不用""的話 ID:1 會false

留言