doushi1974 2014-11-03 21:03
浏览 23
已采纳

字符串混淆1)或者如果是cond。 并且strtolower不降低

Trying to convert times such as - 215pm or 2:15 am or whatever to a 14:15 or 02:15 type string. (I am NOT using a TIME format for reasons to tedious to go into.)

Have managed to get an array output that contains hours, minutes and am/pm (but only if characters are present).

I want to allow erroneous characters through so I can EXPLICITLY fire an error message but a) I cannot get a "simple" OR test to work and b) strtolower is not working for me - I don't know why.

$hour= $matches['h'];
$minute=$matches['m'];

if(!array_key_exists('ap', $matches)){$matches ['ap'] ="am";};
$ampm=$matches['ap'];
strtolower ($ampm);

  debug ($hour,"hr");
  debug ($minute,"min");
  debug ($ampm,"ampm");

if ($ampm=="am" or $ampm=="pm") {echo "fine";}; // ALWAYS TRUE

if ($ampm==="am" or $ampm==="pm") {echo "fine";}; // FAILS WITH UPPER CASE AM AND PM

if ($hour<12 and $ampm=="pm") {$hour = $hour+12;};
  debug ($hour,"hr");
  debug ($minute,"min");
  debug ($ampm,"ampm");

So I do not understand why the simple string test $ampm=="am" or $ampm=="pm" is always true but that is just academic. But I REALLY do not understand why strtolower does not change "AM" to "am" so that I cannot even use a === congruence test.

Any thoughts?

  • 写回答

2条回答 默认 最新

  • dongxian3574 2014-11-03 21:07
    关注

    Do this to replace the variable:

    $ampm = strtolower($ampm);
    

    Also, $ampm is always true because the condition says

    "if it is am, it is true OR if it is pm, it is true"

    so what you want to do is:

    if(isset($ampm) && $ampm === "am")
    {
        //its am
    } else if(isset($ampm) && $ampm === "pm")
    {
        //its pm
    } else
    {
        //its neither
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)