/    Sign up×
Community /Pin to ProfileBookmark

Variable problems

I can’t see why this simple script wont work?

PS I’m very new to PHP

#form.html
<html>
<head>
</head>
<body>
<form action=”form.php” method=”post”>
<p>Name: <input type=”text” name=”name” size=”20″ maxlength=”50″ /></p>
<input type=”submit” name=”submit” value=”submit” />
</form>
</body>
</html>

#form.php
<html>
<head>
<title>guestbook</title>
</head>
<body>
<?php
echo “Thank you $_POST[‘name’] for your comment!”;
?>
</body>
</html>

I get this:
Parse error: parse error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /home/gina/public_html/guest/form.php on line 7

Line 7 being the line beginning echo

Thank you! ?

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@NogDogJun 17.2005 — [code=php]
echo "Thank you {$_POST['name']} for your comment!";
[/code]

The PHP parser handles scalar variables (e.g.: $varname) within a quoted string no problem, but it tends to get confused by array elements. The {$...} format explicitly tells the parser to read everything between the curly braces as a variable to be interpolated.
Copy linkTweet thisAlerts:
@FunkymonkeyauthorJun 17.2005 — Great thank you!!!!

So simple...... ? ?
Copy linkTweet thisAlerts:
@ShmohelJun 17.2005 — I know it is not necessary, but for my own personal sake and to keep formatting cdonsistent throuought my pages so my text editor color codes everything in a similar manner, I usually prefer to close out quotes before adding variables:

[code=php]
echo "Thank you ".$_POST['name']." for your comment!";
[/code]
Copy linkTweet thisAlerts:
@NogDogJun 17.2005 — I know it is not necessary, but for my own personal sake and to keep formatting cdonsistent throuought my pages so my text editor color codes everything in a similar manner, I usually prefer to close out quotes before adding variables:

[code=php]
echo "Thank you ".$_POST['name']." for your comment!";
[/code]
[/QUOTE]

Perfectly valid. For some reason I prefer the {$...} thing simply because I dislike using the "." concatenation operator. (It just seems messier to me, I guess?) Whichever one works for you is the best, and it probably does help to be consistent. ?
×

Success!

Help @Funkymonkey spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...