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

Gareth Redfern
Gareth Redfern
36,217 Points

PHP Help Required

In the following code challenge task 2 of 2 Stage 6 Adding Search Model.

I don't understand what I need to do to stop Step 1 from failing, if search_term equals vanilla then the else runs but if search_term equals cookie then the first part of the if runs, which is correct.

The hint is to modify the conditional but how?

Jordan Clarke
Jordan Clarke
23,464 Points

Hi Gareth, I'm not sure how it's failing. I'm assuming you've passed step 1 by switching the 'stripos' attributes so they're in the correct order, nothing more needs to be done there. Then step 2, you're changing cookie to vanilla on the $search_term. Switching between cookie and vanilla just changes the way the if/else proceeds. Is it showing an error message?

Gareth Redfern
Gareth Redfern
36,217 Points

Hi Jordan, thanks for the assist, so here is my code for step 1 which passes:

<?php

    $flavor = "Cookie Dough";
    $search_term = "cookie";

    if (str_ireplace($search_term,$flavor) !== false) {

        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 . "'.";

    }

?>

All I am doing in step 2 is replacing $search_term = "cookie"; with $search_term = "vanilla"; which then gives the following error.

Jordan Clarke
Jordan Clarke
23,464 Points

Right ok, where you have (str_ireplace($search_term, $flavor), this needs to be (stripos($flavor,$search_term).

Notice how the $search_term and $flavor are reversed; 'stripos' searches for a term in a string. The string you are searching, needs to be the first attribute in the function. The second attribute is the search term itself.

So you want to search 'Cookie Dough' for the word 'cookie'.

Once you've done that, step 2 should then pass as you've done it.

Does that make sense?

Gareth Redfern
Gareth Redfern
36,217 Points

That worked perfect, thank you for taking the time to help on this Jordan! I think I had got confused with which function to use as I shouldn't have been using str_ireplace().

Thanks again!

Jordan Clarke
Jordan Clarke
23,464 Points

No problem Gareth. Happy to help :-)

Gareth Redfern
Gareth Redfern
36,217 Points

Hi Jordan, can you post your answer as an answer rather than just a comment so I can mark it as the best answer?

5 Answers

Jordan Clarke
Jordan Clarke
23,464 Points

Right ok, where you have (str_ireplace($search_term, $flavor), this needs to be (stripos($flavor,$search_term).

Notice how the $search_term and $flavor are reversed; 'stripos' searches for a term in a string. The string you are searching, needs to be the first attribute in the function. The second attribute is the search term itself.

So you want to search 'Cookie Dough' for the word 'cookie'.

Once you've done that, step 2 should then pass as you've done it.

You need to have double equalssign == otherwise it's not a conditional, it assigns the value to search_term. And if search_term is a variable it should have a dollar sign, like this $search_term.

Gareth Redfern
Gareth Redfern
36,217 Points

Hi Joakim, sorry I realise now how I had written my question could have been confusing, I have changed the wording. I think the only way for you to see exactly what I mean is to review the code challenge.

Hey Gareth you could actually concatenate it take a look at the example I posted below. Though I don't think that is the best practice but i think it's the easiest to pass the test and achieve the same result.

<?php

    $flavor = "Cookie Dough";
    $search_term = "cookie";

    if (stripos($search_term.$flavor) !== false) {

        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 . "'.";

    }

?>
Gareth Redfern
Gareth Redfern
36,217 Points

Thanks, Jordan that worked for me.