Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

PHP Enhancing a Simple PHP Application Adding Search: Model Using stripos: Starts with R

Task 2 of stripos not passing

I have tried this a lot but its not working. Basically its asking for us to fix the error like needle in the haystack. But as in my example its not. why? the $var1 and $var2 both starts with R, hence meeting the condition of finding R. Also if i want to write the code down, how can i put it to explain. myself before if <br> " //if var1 starts from R and is true then do this"

index.php
<?php

    $var1 = "Rocky Road";
    $var2 = "Raspberry";
    $initial = "R";

    if (!stripos($var1,$initial) == 1 && stripos($var2,$initial) == 1) {

        echo $var1 . " and " . $var2 . " both start with " . $initial . ".";

    } else {

        echo "Either " . $var1 . " or " . $var2 . " doesn't start with " . $initial . "; maybe neither of them do.";

    }

?>

4 Answers

Shane Meikle
Shane Meikle
13,188 Points

It uses a 0 based index, meaning it begins counting at 0 so the R's would be at position 0. Change the 1's to 0's and it should give you the alternate condition.

Shane Meikle Thanks! and what about if i want to understand the code in plain english, would i say something like "if var1 starts from R and is true then do this"?

Shane Meikle
Shane Meikle
13,188 Points

Yes, that would be about right for the first condition (before the else)

Thanks

The other thing you have to use is ===. If you use == a return of 0 is the same as false and your code may not work properly. This function returns an integer value for the position of the searched term or false if the letter is not there. If the search character is the first letter, it returns 0 which is equal to false if you use == and not ===.