petrovicstefanrs/30_seconds_of_knowledge

Snippet error

Closed this issue · 2 comments

Hello.

I think this snippet is wrong :
https://github.com/petrovicstefanrs/30_seconds_of_knowledge/blob/master/src/assets/snippets/php/isContains.md

function isContains($string, $needle)
{
    return strpos($string, $needle);
}

strpos function can return int or false. If you do :

if (isContains('abcd', 'abc')){
}

Condition is false because (bool) 0 === false.

@coffeecore Yep, nice catch! What do you think if I just fix it to this:

function isContains($string, $needle)
{
    return strpos($string, $needle) != -1;
}

I think better way :

function isContains($string, $needle)
{
    return false !== strpos($string, $needle);
}