开发者

if-problem with dot

开发者 https://www.devze.com 2023-01-08 00:43 出处:网络
Following code: 开发者_运维知识库<?php $str = \"19.09.02\"; if(substr($str, -3, 2) == \".0\")

Following code:

开发者_运维知识库<?php
$str = "19.09.02";
if(substr($str, -3, 2) == ".0")
{
    // Doing something
}

$str2 = "19.09.2002";
if(substr($str2, -3, 2) == ".0")
{
    // Doing something
}
?>

Why does the second statement apply (without regexp)? and how can I solve, that it just apply the first expression?

Thank you


I think you should use the identity (===) operator to fix this :)

One of the main differences of === vs == is that === doesn't cast at all, it is a very strict comparison.


Just write === instead of ==

you could also use something like:

if(preg_match("!([0-9]{2}).([0-9]{2}).([0-9]{2,4})!", $str, $found)){
    list($day, $month, $year) = array_shift($found);

    if(strlen($year)==4){
        //must be 4
    }else{
        //must be 2...
    }
}


Try === operator. The problem is that in both the cases the value returned is 0 and it is being compared with another 0.

0

精彩评论

暂无评论...
验证码 换一张
取 消