polarbear22 Posted May 15, 2014 Share Posted May 15, 2014 Hey guys I just made an account on here, I might be getting an internship soon and I haven't had much experience with php. (I have some in other languages tho) So i'm using a site called codecademy to get me started with the bare basics. I'm getting a weird error from their emulator and i'm not sure whats wrong with this code From what I can tell by the google searches i've done it meets proper syntax. I was wondering if anyone can tell me why this isn't working? <html> <p> <?php // Print out the position of a letter that is in // your own name strpos("John", "h"); ?> </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("John","K") === false){ print "Sorry no 'k' in John."; } ?> </p> </html> Any info and help is greatly appreciated! Quote Link to comment https://forums.phpfreaks.com/topic/288520-can-you-help-me/ Share on other sites More sharing options...
requinix Posted May 15, 2014 Share Posted May 15, 2014 It's definitely valid, though there is a bug with it. What error are you getting? Quote Link to comment https://forums.phpfreaks.com/topic/288520-can-you-help-me/#findComment-1479598 Share on other sites More sharing options...
ginerjm Posted May 15, 2014 Share Posted May 15, 2014 Since you are learning here's a sample of code that IMHO is a much better way to develop. It makes for easier reading and debugging and for fewer typos when trying to enter/exit php mode constantly. The idea is to separate html and js from php and let the php vars bring the dynamic parts of the output into the html after being built somewhere else. // always turn on error checking during devl. error_reporting(E_ALL | E_STRICT | E_NOTICE); ini_set('display_errors', '1'); // GET the position of a letter that is in // your own name $name = "John"; $letter1 = "h"; // the function strpos returns a value which you weren't retrieving. $pos1 = strpos($name, $letter1); if ($pos1 !== false) $msg1 = "The letter '$letter1' is at position ".($pos1 +1); // Check for a false value of a letter that is not // in your own name and print out an error message $letter2 = "K"; if (strpos($name,$letter2) === false) { $msg2 = "Sorry - no '$letter2' in '$name'."; } // Now use the assigned vars above in your html and avoid // having to mix php with html. // The PHP 'heredocs' operand (?) is great for outputting lots of html mixed with php vars. Check it out in the manual $code=<<<heredocs <html> <p> $msg1 </p> <p> $msg2 </p> </html> heredocs; echo $code; exit(); Quote Link to comment https://forums.phpfreaks.com/topic/288520-can-you-help-me/#findComment-1479602 Share on other sites More sharing options...
requinix Posted May 15, 2014 Share Posted May 15, 2014 Mind the undefined variable warnings, though. Quote Link to comment https://forums.phpfreaks.com/topic/288520-can-you-help-me/#findComment-1479605 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.