dqzve68846 2012-11-21 21:30
浏览 17
已采纳

如何正确解析PHP中的整数?

I can use intval, but according to the documentation:

Strings will most likely return 0 although this depends on the leftmost characters of the string. The common rules of integer casting apply.

... and the value to parse can be 0, that is I will not able to distinguish between zero and a string.

$value1 = '0';
$value2 = '15';
$value3 = 'foo'; // Should throw an exeption

Real question is: how can I parse the string and distinguish between a string that cast to 0 and a zero itself?

  • 写回答

2条回答 默认 最新

  • doumalu9257 2012-11-21 21:33
    关注

    In the code below, $int_value will be set to null if $value wasn't an actual numeric string (base 10 and positive), otherwise it will be set to the integer value of $value:

    $int_value = ctype_digit($value) ? intval($value) : null;
    if ($int_value === null)
    {
        // $value wasn't all numeric
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分