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 trialGareth Redfern
36,217 PointsPHP 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?
Gareth Redfern
36,217 PointsHi 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
23,464 PointsRight 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
36,217 PointsThat 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
23,464 PointsNo problem Gareth. Happy to help :-)
Gareth Redfern
36,217 PointsHi 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
23,464 PointsRight 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.
Joakim Bergman
14,501 PointsYou 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
36,217 PointsHi 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.
J.L. Sparreboom
3,241 PointsHey 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
36,217 PointsCool, thanks J.L. :)
Gareth Redfern
36,217 PointsThanks, Jordan that worked for me.
Jordan Clarke
23,464 PointsJordan Clarke
23,464 PointsHi 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?