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 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法