If condition not working with strpos

1.3k views Asked by At

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

6

There are 6 answers

0
Bartosz Zasada On BEST ANSWER

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).

0
Vasil Rashkov On

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.

0
Hikmat Sijapati On

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); 
        }
    }
}
0
SamBremner On

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.

0
Mohd Waseem On

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 "";
}

}

0
Soni Vimalkumar On

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;
}