petrovicstefanrs / 30_seconds_of_knowledge

Google Chrome Extension that lets you gain new developer skills, every time you open a New Tab.
https://30secondsofknowledge.com/
MIT License
942 stars 121 forks source link

Snippet error #47

Closed coffeecore closed 5 years ago

coffeecore commented 5 years ago

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.

petrovicstefanrs commented 5 years ago

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

function isContains($string, $needle)
{
    return strpos($string, $needle) != -1;
}
coffeecore commented 5 years ago

I think better way :

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