If condition not working with strpos

大兔子大兔子 提交于 2020-06-29 11:52:06

问题


I created custom function OutputMessage from where i'm inserting error message with its ClassStyle like this Error: image upload failed! and then i'm exploding string and split class from it add in to div class but my function is not working fine.

function OutputMessage($Message=''){
    if($Message){
        $Postion = strpos($Message,":");
        if($Postion !== TRUE){
            return sprintf('<div class="alert alert-default">%s</div>',$Message); 
        }else{
            $Message = explode(": ",$Message);
            return sprintf('<div class="alert alert-%s">%s</div>',strtolower($Message[0]),$Message[1]); 
        }
    }else{
        return "";
    }
}

$Position check is not working because i'm passing Message with it's class but it's still returning default class


回答1:


From the manual entry of strpos() function:

Returns the position of where the needle exists relative to the beginning of the haystack string (independent of offset). Also note that string positions start at 0, and not 1.

Returns FALSE if the needle was not found.

which means that if($Postion !== TRUE) will always be true, as strpos() never returns true.

To make your function work as expected, change your if statement to if($Postion === false).




回答2:


Why you can not achieve like this....

function OutputMessage($Message = NULL){
    if(is_null($Message){
        return;
    }
    else {

        $arr = explode(":",$Message);
        if(count($arr)>0){
            return sprintf('<div class="alert alert-%s">%s</div>',strtolower($arr[0]),$arr[1]); 
        }

       else {
            return sprintf('<div class="alert alert-default">%s</div>',$Message); 
        }
    }
}



回答3:


From the docs of strpos you can see that the function will NEVER return true. Just change it to false in the if statement and everything would work fine.




回答4:


The strpos() function returns the start position of the string as an integer or FALSE if the string does not exist. Your if else statement will therefore never hit the else statement as $Position will never be equal to TRUE.

Swap the if statement to check for FALSE if($Position === FALSE) then you should be able to get the correct behaviour.




回答5:


You can use greater then 0 instant of TRUE

function OutputMessage($Message=''){
if($Message){
    $Postion = strpos($Message,":");
    if($Postion < 0){
        return sprintf('<div class="alert alert-default">%s</div>',$Message); 
    }else{
        $Message = explode(": ",$Message);
        return sprintf('<div class="alert alert-%s">%s</div>',strtolower($Message[0]),$Message[1]); 
    }
}else{
    return "";
}

}




回答6:


Try to achieve with this,

function OutputMessage($Message=''){
    if(is_null($Message) || $Message === ""){ return ""; }

    if(strpos($Message,":") === false){
       $result = sprintf('<div class="alert alert-default">%s</div>',$Message); 
    }else{
        $Message = explode(": ",$Message);
        $result = sprintf('<div class="alert alert-%s">%s</div>',strtolower($Message[0]),$Message[1]); 
    }
    return $result;
}


来源:https://stackoverflow.com/questions/41460859/if-condition-not-working-with-strpos

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!