This forum is now read-only. Please use our new forums at discuss.codecademy.com.

Functions, Part I Forum

View Course » View Exercise

397 points
Picture
Submitted by
Cindy Forehand
almost 3 years ago

String Functions II exercise 3

Why doesn't this code work?


<?php
// Print out the position of a letter that is in
// your own name
$haystack = "Cindy";
$needle = "n";
if (strpos("$haystack","$needle") == true) {
echo "The letter is in position ";
echo strpos ("$haystack", "$needle");
}
?>
<?php
// Check for a false value of a letter that is not
// in your own name and print out an error message
else (strpos("$haystack","$needle") === false) {
echo "Sorry, no '$needle' in '$haystack'";
}
?>


-1 votes

permalink

<?php
// Print out the position of a letter that is in
// your own name
strpos("emily", "e"); {
print "0";
}

?>
</p>
<p>
<?php
// Check for a false value of a letter that is not
// in your own name and print out an error message

if (strpos("david","h") === false) {
print "Sorry, no 'h' in 'david'";
}
?>

93 points
20fd602264c6d26a68f1fc0df387ddc9?s=140&d=retro
Submitted by
BimiMalysh
almost 3 years ago

1 Comment

1cd44f6b22536d9ffeedafda1fa3f056?s=140&d=retro yassin1999 over 2 years ago

DOESNT WORK


-1 votes

permalink

right answer is here . . .. . .

<?php
// Print out the position of a letter that is in
// your own name
print strpos("hardik", "h"); //0
print strpos("hardik", "p"); //false
?>


<?php
// Check for a false value of a letter that is not
// in your own name and print out an error message
if(strpos("hardik", "p") === false) {
print "Sorry, no 'p' in 'hardik'";
}
?>

789 points
54016e4c631fe94c66001020_484326506
Submitted by
Rat94
almost 3 years ago