Question

I've a variable which contains comma separated strings. This is generated dynamically from the array elements by using implode() function.So sometimes it contains nothing, sometimes it contains 1/2/3/4 strings separated by comma. I want to check whether the stirng Other is present within this comma separated string and if it's present then do the commands written inside if. But I'm facing a issue in which it's never detecting the string "Other" inside the comma separated values though tit's present. Can anyone help me in this regard please? For your reference following is my code:

$form_data['que_issue'] = implode(",", $request['que_issue']);

if(strpos($form_data['que_issue'],"Other")) { 
    echo "In If"; 
    die;
    if(!$this->mValidator->validate($form_data['que_issue_comment'], "required", "true"))
        $this->mValidator->push_error($errors_msgs['que_issue_comment_blank'], 'que_issue_comment');
    elseif(!$this->mValidator->validate($form_data['que_issue_comment'], 'maxlength', '100'))
        this->mValidator->push_error($errors_msgs['que_issue_comment_length_invalid'], 'que_issue_comment');
} else
    echo "In a else"; 
die;//Its always going in else part only

Thanks in advance.

Was it helpful?

Solution

strpos will return 0 if string match at the begin of the string. But 0 is interpreted as false by PHP.

Always look the documentation and see what is the specific return type/value when the method "fail" and compare against that. In this case

if(strpos($form_data['que_issue'],"Other") !== false)

OTHER TIPS

you should try something like this

if (strpos($form_data['que_issue'],'Other') !== false) {
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top