duanao3204 2014-01-31 15:20
浏览 90
已采纳

在php中比较字符串与字符串的“枚举”

I wanna compare string with "enumeration", can I do it simplier than in my solution bellow? First I know the enum type is not implemented in php.
Basically the problem question is: Is string s equal to one of the strings? Since php don't have enum, the values of enum could be probably in some array or something like that.


//$min_s is string value of some minute value for example "15".
if((strcmp($min_s, "00")== 0 || str_cmp($min_s,"15") == 0 ||
            strcmp($min_s, "30")== 0 || strcmp($min_s, "45") == 0)
{ // ok}

Goal:
Make my ifs more readable for this specific example and others in future when I wanna compare string to "enum".

  • 写回答

1条回答 默认 最新

  • dtjbcda841554 2014-01-31 15:39
    关注

    To answer your question literally:

    $fifteens = array('00', '15', '30', '45');
    if (in_array($min_s, $fifteens)) {
        ...
    }
    

    But I would actually use arithmetic:

    if ($min_s % 15 == 0) {
        ...
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题