Copied & Paste this example from a page, getting errors(PHP)

Discuss how to write good code, break bad code, your current pet projects, or the best way to approach novel problems

Copied & Paste this example from a page, getting errors(PHP)

Post by coldghost213 on Mon Jul 14, 2008 1:21 pm
([msg=7408]see Copied & Paste this example from a page, getting errors(PHP)[/msg])

Code: Select all
<html>
<head></head>
<body>

<?php
/* if the "submit" variable does not exist, the form has not been submitted - display initial page */
if (!isset($_POST['submit'])) {
?>

    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
    Enter your age: <input name="age" size="2">
    <input type="submit" name="submit" value="Go">
    </form>

<?php
    }
else {
/* if the "submit" variable exists, the form has been submitted - look for and process form data */
    // display result
    $age = $_POST['age'];
    if ($age >= 21) {
        echo 'Come on in, we have alcohol and music awaiting you!';
        }
    else {
        echo 'You're too young for this club, come back when you're a little older';
    }
}
?>

</body>
</html>


This is the code I copied and pasted from http://devzone.zend.com/node/view/id/628 and when I attempted to run it on Apache Server 2.2 (Using Php 5.0), I got this error


Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\allinone.php on line 25


I seen this error before when I forgot to add ; in, but from the limited knowledge I have from php, I don't see what's wrong with this. Any help?
coldghost213
New User
New User
 
Posts: 15
Joined: Wed Jun 25, 2008 9:04 pm
Blog: View Blog (0)


Re: Copied & Paste this example from a page, getting errors(PHP)

Post by BhaaL on Tue Jul 15, 2008 11:20 am
([msg=7474]see Re: Copied & Paste this example from a page, getting errors(PHP)[/msg])

Take a look at line 25:

Code: Select all
       echo 'You're too young for this club, come back when you're a little older';


That string is enclosed by single quotes '
And the string itself uses single quotes aswell as apostrophes - they arent escaped, for that reason, they end the string.
PHP sees this:

Code: Select all
echo 'You'<not a string anymore, should be code>'re a little older';

Because "re too young..." is not syntactically correct there, it throws the error.

Try this:

Code: Select all
echo 'You\'re too young for this club, come back when you\'re a little older';

Escaping the ' with \ tells PHP that a special character follows, which is to be taken literally.
There are also other ways to solve that, but I guess you can figure them out yourself :P
BhaaL
Poster
Poster
 
Posts: 270
Joined: Sun Apr 13, 2008 11:16 am
Blog: View Blog (0)


Re: Copied & Paste this example from a page, getting errors(PHP)

Post by coldghost213 on Tue Jul 15, 2008 11:27 am
([msg=7475]see Re: Copied & Paste this example from a page, getting errors(PHP)[/msg])

BhaaL wrote:Take a look at line 25:

Code: Select all
       echo 'You're too young for this club, come back when you're a little older';


That string is enclosed by single quotes '
And the string itself uses single quotes aswell as apostrophes - they arent escaped, for that reason, they end the string.
PHP sees this:

Code: Select all
echo 'You'<not a string anymore, should be code>'re a little older';

Because "re too young..." is not syntactically correct there, it throws the error.

Try this:

Code: Select all
echo 'You\'re too young for this club, come back when you\'re a little older';

Escaping the ' with \ tells PHP that a special character follows, which is to be taken literally.
There are also other ways to solve that, but I guess you can figure them out yourself :P


Thanks :)
$tutorialSites != "fools proof"

Lol had to say that >.<

BTW: I also liked how you explained it instead of slap me the answer like some people do. Thanks :3

also tried inserting double quotes (") instead of single quotes (') to state the beginning of the string also seems to work. Though it'll probably be less confusing to just add the \
coldghost213
New User
New User
 
Posts: 15
Joined: Wed Jun 25, 2008 9:04 pm
Blog: View Blog (0)



Return to Programming

Who is online

Users browsing this forum: No registered users and 0 guests