Skippy wrote:
On Mon, 19 Jul 2004 14:15:09 -0400 Jason Barnett <[EMAIL PROTECTED]>
wrote:
Markus Stobbs wrote:
$message = "
Name: $Name
Division: $Division
Phone: $Phone
Email: $Email";
...but when I change $Name and the other variables to $_POST['Name'], I
get this error:
Parse error: parse error, unexpected T_ENCAPSED_AND_WHITESPACE,
expecting T_STRING or T_VARIABLE or T_NUM_STRING in
/web/scd/vets/Vislab/eventrequest.php on line 94
When you have a variable that is inside a text string (double quotes) like
that then you do not need to have the quotes for your array index. So in
your case something like this should work:
$message = "
Name: $_POST[Name]
Division: $_POST[Division]
Phone: $_POST[Phone]
Email: $_POST[Email]";
I think this is just solving one bad practice (inserting array elements
directly in strings) with another (letting undefined constants such as Name
pass as variable values).
The problem for Markus is that there's a limit to what kind of variables you
can use directly inside strings. In particular, as he noticed, array elements
referred by association ($_POST['Name']) won't work.
Your workaround uses a trick: by not enclosing the element identificator
(Name) in quotes, it is considered to be a constant. Since there's no
constant with that name defined, it evaluates to the name of the constant,
which is Name. This is bad practice; the manual page for constants
specifically calls it that. And because you don't use quotes to delimitate
the identificator, you don't have Markus's problem.
The proper solution is to use string concatenation and thus place your
variables outside of the string:
$message = "
Name: ".$_POST['Name']."
Division: ".$_POST['Division']."
Phone: ".$_POST['Phone']."
Email: ".$_POST['Email'];
It's good habit to restrain yourself from both the above bad practices.
Try to always use concatenation and specifically put variables outside of
strings, and always use the quotes for array identificators. Once the habit
is in you'll have less headaches to worry about.
Hello.
Skippy, you ought to read this:
http://www.php.net/manual/en/language.types.string.php#language.types.string.parsing.simple
Explains how PHP parses variables (including arrays) inside strings.
Daniel
--
WWE e-commerce IT GmbH
Eiffestrasse 462, D-20537 Hamburg
Tel.: +49-40-2530659-0, Fax: +49-40-2530659-50
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php