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 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料