Jump to content

Can you help me?


polarbear22

Recommended Posts

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! 

Link to comment
Share on other sites

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();
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.