This forum is now read-only. Please use our new forums! Go to forums

banner
Close banner
0 points
Submitted by Pseudokitten
almost 9 years

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

Permalink

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

points
Submitted by Pseudokitten
almost 9 years

1 comments

Motorcycleboy almost 9 years

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.

Answer 559c13249113cb130a00036f

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";
    }`
points
Submitted by selath
almost 9 years