My code is producing an error that my strpos() shold produce a number


#1



 <?php
    // Print out the position of a letter that is in
    // your own name
    strpos("marcus", "m");  //0
    strpos("marcus", "a");  //1
    strpos("marcus", "rcus"); //1
    strpos("marcus", "pty"); //false
    ?>
    </p>
    
    <p>if (strpos("marcus","t") === false) {
        print "sorry, no 't' in 'marcus'";
    }
    <?php


#2

HI this part

if (strpos("marcus","t") === false) {
        print "sorry, no 't' in 'marcus'";

Should be inside the tag <p></p>


#3

Hi there your second part of the code seems ok except its missing </p>
The error is for the first part of your code, just include 'print' in the first part and it will print the numbers.
For example

<p>
 <?php
    // Print out the position of a letter that is in
    // your own name
   print strpos("marcus", "m");  //0
   print strpos("marcus", "a");  //1
   print strpos("marcus", "rcus"); //1
    ?>
    </p>

#4

Getting the same error. Can someone please explain why?

<html>
    <p>
    <?php
    // Print out the position of a letter that is in
    // your own name
    $myName = "david";
    $find = "w";
    strpos($myName, $find);
    ?>
    </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($myName, $find) === false){
        print "Nop, there is no " + $find + " in " + $myName + ".";    
    }
    ?>
    </p>
</html>

#6

Hi the first part

<p>
<?php
    // Print out the position of a letter that is in
    // your own name
    $myName = "david";
    $find = "w";
    strpos($myName, $find);
?>
</p>

the first one should be true so that it will print a number so you should change

$find = "w"

the letter "w" with any letter in your name..
then the second part

    <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($myName, $find) === false){
        print "Nop, there is no " + $find + " in " + $myName + ".";    
    }
    ?>
    </p>

its should be false so you should the variable $find and put another variable with any letter which isn't in your name

Hope that could help you


#7

Thanks for the response.

Though, your explanation for the second part doesn't make sense.

I can make the first part work no problem.
I changed the first part to be a letter Not in my name so I could get the second part working. But still, same error.

Is it a scope issue?


#8

Never mind.

The issue is print needs to be used with strpos.


#9

I mean like that

<html>
    <p>
    <?php
    // Print out the position of a letter that is in
    // your own name
    $myName = "david";
    $find = "d";#I put d as letter so in this case:
    echo strpos($myName, $find); #this will be true and will print a number
    ?>
    </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($myName, $find) === false){ #But thsi one will not be true because it shoudl results false so that the equality will be true 
        print "Nop, there is no " + $find + " in " + $myName + ".";    
    }
    ?>
    </p>
</html>

In the if statement the equality should result true but with

strpos("david", "d") # is true

in the if statement

if (true === false) #the if statement will be false

but if you do that

strpos("david", "w") #is false

#the if statement
fi (fasle === fasle) #the if statement will be true

Does it make sense to you?
so if you want to work you could do something like that

<html>
    <p>
    <?php
    // Print out the position of a letter that is in
    // your own name
    $myName = "david";
    $find = "d";
    strpos($myName, $find);#this is true
    #after that stropos I change the value of the variable $find
    $find = "w"
    ?>
    </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($myName, $find) === false){ #this is also true 
        print "Nop, there is no " + $find + " in " + $myName + ".";    
    }
    ?>
    </p>
</html>

#10

How did you make the first part work. I Couldn't do it either.

<html>
<?php
echo strpos("Akash","s");;
?>
<p></p>
<?php
if (strpos("Akash","g") == false){
echo "Sorry, We couldn't g in Akash";
}
?>
</html>

This is my code for the problem. I am getting the same error "My first strpos() call should output a number".