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 C

Alberto Centelles
Alberto Centelles
8,356 Points

I have this block of code that isn’t working correctly, but I can’t figure out the problem. It says that Cookie Dough an

Why does it display "Cookie Dough and Vanilla both start with C"?

<?php

$var1 = "Cookie Dough";
$var2 = "Vanilla";
$initial = "C";

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

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

} else {

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

}

?>

6 Answers

Hi Alberto,

The problem with the starter code is that it uses loose comparison == This means type juggling will occur in order to see if the two sides are equal.

stripos will return an index number if there's a match or FALSE if there's no match. 0 is considered a FALSE value so with a loose comparison you won't be able to tell the difference of whether 0 was returned or FALSE was returned.

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

stripos($var1,$initial) is going to return 0 because a match is found at the beginning but stripos($var2,$initial) will FALSE because no match is found.

So you have:

if (0 == 0 && FALSE == 0) {

Both of these comparison are true because it's a loose comparison. Types will be converted in an attempt to get them equal.

You want to use the identity comparison ===

Now you have:

if (0 === 0 && FALSE === 0) {

Here the right comparison will be FALSE because they are not strictly equal. They have to match in both value and type. So the else block will be correctly executed.

You always want to use === with stripos() because otherwise, you can't tell the difference between a match at the beginning and no match at all.

There you have it. Give this guy twelve points Best Answer!

Thanks Jeff.

Travis Stewart
Travis Stewart
15,188 Points

Thanks. I was pretty hung up on this one.

Hi Alberto,

Change one of the zeros in the if statement to 1 or anything but zero.

Jeff

<?php

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

?>
Alberto Centelles
Alberto Centelles
8,356 Points

Thanks Jeff! But, why is that? Why can't it be 0?

Hi Alberto,

I'm sorry, I wasn't thinking, the correct code is below.

Jeff

<?php

if (stripos($var1,$initial) == "C" && stripos($var2,$initial) == "C") {

?>
Alberto Centelles
Alberto Centelles
8,356 Points

But this function (stripos) gives you an integer or a boolean. It will give you a string, isn't it? The code worked, though

Alberto Centelles ,

I I did some searching around and the code below is correct, I believe. I don't fully understand why, but I will.

<?php

    $var1 = "Cookie Dough";
    $var2 = "Chocolate";
    $initial = "C";

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

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

    } else {

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

    }

?>