douliudong8108 2014-05-06 09:57
浏览 212
已采纳

is_numeric函数给出了意想不到的结果

I wrote the following php code,

<?php
 $k ="123e5";
if(is_numeric($k)){
    echo "is number";
}
else{
    echo "is not a number";
}
?>

the expected out put is "is not a number" because e is present in the string. But I got the output "is number". why is that happens? and please help me to find the solution.

  • 写回答

3条回答 默认 最新

  • duanmao2650 2014-05-06 10:03
    关注

    You can use ctype_digit() to check for numeric character(s)

    http://www.php.net/manual/en/function.ctype-digit.php

    <?php
    
    $numeric_string = '42';
    $integer        = 42;
    
    ctype_digit($numeric_string);  // true
    ctype_digit($integer);         // false (ASCII 42 is the * character)
    
    is_numeric($numeric_string);   // true
    is_numeric($integer);          // true
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 蓝牙耳机怎么查看日志
  • ¥15 Fluent齿轮搅油
  • ¥15 八爪鱼爬数据为什么自己停了
  • ¥15 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏