Re: Re: Mixing $_POST with text in a variable
| From: | Marek Kilimajer | Date: | Mon, 19 Jul 2004 21:16:30 +0000 |
| Subject: | Re: Re: Mixing $_POST with text in a variable | ||
| References: | 1 2 3 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-191075@lists.php.net to get a copy of this message | ||
Skippy wrote:
On Mon, 19 Jul 2004 14:15:09 -0400 Jason Barnett <jasbarne@indiana.edu> wrote:Not quite trueMarkus Stobbs wrote: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.$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 94When 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]";
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.It is completely legal. Name, Division, Phone and Email are not constants, because they are in a string: "Name: $_POST[Name]";
^^^^ ^^^^ | | string string