On Mon, 19 Jul 2004 22:01:01 +0300, Skippy <[EMAIL PROTECTED]> 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.
> 

And if you follow the concatenation idea, you should also use single
quotes around your strings unless you need things like \n and \t.
Single quotes are not parsed for backslash replacements (except \\ and
\') and variables, so they give a slight performance increase.

-- 
DB_DataObject_FormBuilder - The database at your fingertips
http://pear.php.net/package/DB_DataObject_FormBuilder

paperCrane --Justin Patrin--

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to