This forum is now read-only. Please use our new forums! Go to forums
banner
Close banner
Why is my code not reading the first position (0) in the string?
Having a little trouble here, it seems my second piece of code is returning that there is no R in the name, (the condition is false). The top piece of code returns both 0 and 0 for the string position, since php works on the assumption 0 is 1. The bottom piece does not see the R unless I put a space in the $myname. It’s effectively ignoring the first position.
<?php
// Print out the position of a letter that is in
// your own name
$myname = "riley";
$charpos = strpos("$myname","r");
echo $charpos;
$charpos2 = strpos("$myname", "r");
echo $charpos2;
?>
<?php
// Check for a false value of a letter that is not
// in your own name and print out an error message
$falseletter = "r";
if (strpos("$myname","$falseletter") == false) {
echo "There is no {$falseletter} in your name";
}
else {
print "That letter is in your name!";
}
?>
Answer 556d2ec2d3292fd90b000083
1 vote
Seems like I made the mistake of having “== false” instead of “=== false”. Why is the 3rd equals sign significant?
Answer 559c13249113cb130a00036f
0 votes
yeah i had the same problam here’s my code:
`<?php
// Check for a false value of a letter that is not
// in your own name and print out an error message
$letterposi = strpos("selath","s");
if($letterposi == false){
print "sorry it doesn't exist";
}
else{
print "your letter position is $letterposi";
}`
Popular free courses
- Free Course
Learn SQL
In this SQL course, you'll learn how to manage large datasets and analyze real data using the standard data management language.Beginner friendly,4 LessonsLanguage Fluency - Free Course
Learn JavaScript
Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.Beginner friendly,11 LessonsLanguage Fluency - Free Course
Learn HTML
Start at the beginning by learning HTML basics — an important foundation for building and editing web pages.Beginner friendly,6 LessonsLanguage Fluency
1 comments
It’s different in all languages. For example in java when a variable becomes something you use only one =. If you’re using an if statement then and comparing things then it would be == in java. Hopefully that kind of helps.