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

Functions, Part I Forum

View Course » View Exercise

65 points
Picture
Submitted by
Gabriel Oggier
almost 2 years ago

why isn't my strpos not working?

My strpos is not working.... mi code:

<?php
// Print out the position of a letter that is in
// your own name
strpos("gabriel", "l");
?>


2 votes

permalink

<?php
//...
//...
echo strpos("gabriel", "l");
?>

You need to echo your strpos, or else it won't show anything in the console.

1321 points
B64f1bafbdb8f6b1fa21e962e3377456?s=140&d=retro
Submitted by
Ben
almost 2 years ago

1 Comment

Picture Gabriel Oggier almost 2 years ago

Thanks!


0 votes

permalink

I added comments so its easier to follow and understand rather than just coping code and pasting it for the sake of moving on. So it will help weather you copy it or write your own. Happy coding!

<?php
// Print out the position of a letter that is in
// your own name

$myName = strpos("Ntokozo", "z"); // $myName is the variable. strpos serves to only identify the position of the identified letter next to the name. Both need to be in seperate quotes.


print $myName // Prints strpos in the variable selected. therefore will print "z" as 5, as a result of the "z" in quotes because its in position 5 when counting from 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("Ntokozo", "C") == false) {
print "Sorry, no 'C' in 'Ntokozo'";}    
//If strpos does not contain a letter in the word, print the following message.
?>
</p>

471 points
359232948d982bd97142d97a140228cd?s=140&d=retro
Submitted by
Ntokozo
almost 2 years ago

3 Comments

C44eb662ebae69f03c7837aa06ab2071?s=140&d=retro Duncan almost 2 years ago

Out of sheer desperation I tried copying and pasting your code. the preview window works (as it did when I tried my own code) but still get error Oops, try again. Your first strpos() call should output a number.. Wonder if its a Browser issue.

C44eb662ebae69f03c7837aa06ab2071?s=140&d=retro Duncan almost 2 years ago

UPDATE: the code above (and my original code) worked fine in Firefox (36.0.1), just not Chrome Browser (Version 45.0.2454.101 (64-bit)). Sorry, should have checked this before posting.

359232948d982bd97142d97a140228cd?s=140&d=retro Ntokozo almost 2 years ago

Its okay Duncan. I at times get frustrated too and having to change browsers every now and then adds to that frustration. I'm using Chrome oddly enough, but it works. As long as you can understand the code, that's all that matters. Happy coding!