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

Functions, Part I Forum

View Course » View Exercise

179 points
51f067478c1ccc1c42000c18_515939322
Submitted by
Pseudokitten
about 2 years ago

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!";
}
    ?>

1 vote

permalink

Seems like I made the mistake of having "== false" instead of "=== false". Why is the 3rd equals sign significant?

179 points
51f067478c1ccc1c42000c18_515939322
Submitted by
Pseudokitten
about 2 years ago

1 Comment

556e07ba76b8fe8b4b00054f_221051737 Motorcycleboy about 2 years ago

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.


0 votes

permalink

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";
    }`

140 points
9c1afc843d595ca72756f3f89f138bb5?s=140&d=retro
Submitted by
selath
about 2 years ago