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
Christopher Reed
12,243 PointsAdding Search: Model
Task 2 of this challenge asks me to change $var1 to Rocky Road and $var2 to Raspberry.....but when I do, it just says that task 1 is no longer passing. Task 1 has variables $var1 and $var2 as Broccoli and Cream Soda. Am I missing something?
TASK 1:
<?php
$var1 = "Broccoli";
$var2 = "Cream Soda";
$initial = "R";
if (strpos($var1,$initial) == 1 && strpos($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.";
}
?>
TASK 2:
<?php
$var1 = "Rocky Road";
$var2 = "Raspberry";
$initial = "R";
if (strpos($var1,$initial) == 1 && strpos($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.";
}
?>
2 Answers
Mike Costa
Courses Plus Student 26,362 PointsThe name of this challenge is "using stripos: starts with R". The function "stripos" returns the position of the character in the string you're searching for. The i in the function name stands for case insensitivity. The reason task 1 passed for you was because you were using the other strpos function (which is case sensitive) and checking for the second character in the string. (B would be 0, r would be 1, o would be 2, c would be 3, ect ect).
So by changing the functions from stripos to strpos, the answer you get is the correct one, but task 2 will make you fail because you're no longer abiding by what the question is asking of you.
In order to check for the first element of the string, replace the 1 (which is the second letter in the string) with a 0 (which would be the first element).
if (stripos($var1,$initial) == 0 && stripos($var2,$initial) == 0)
Hope that helps clear up the confusion of why task 1 passed and the second one failed.
Christopher Reed
12,243 PointsThanks Mike! I understand it now!