Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP Enhancing a Simple PHP Application Adding Search: Model Using strpos: Searching for Cookie

How do i make sense of what the condition say?

First, i don't know if i am making sense of code in plain english and even if i am, its not working. What am i missing?

index.php
<?php

    $flavor = "Cookie Dough";
    $search_term = "vanila";
    //if string $search_term is in $flavour and is not false
    if (stripos($search_term,$flavor) !== false) {
    //then
        echo "Randy's favorite flavor of ice cream contains the search term '" . $search_term . "'.";

    } else {

        echo "Randy's favorite flavor of ice cream does NOT contain the search term '" . $search_term . "'.";

    }

?>

You have your stripos functions reversed. The array goes first and the second term is the search term.

http://php.net/manual/en/function.stripos.php

To answer the logic question, if the haystack has a needle, then favorite ice cream is there. Else, haystack does not have needle.

Theodore Sumner Reversed how? needle in the haystack, its that order for same strpos ad stripos

1 Answer

From the PHP.net entry on stripos:

mixed stripos ( string $haystack , string $needle [, int $offset = 0 ] )

Haystack is first, then needle. The expression in common use is needle in a haystack, but the syntax for the code is search the haystack for a needle.