[nycphp-talk] How do I evaluate variables in a heredoc?
Dan Cech
dcech at phpwerx.net
Mon Jan 5 23:30:42 EST 2004
You need to use curly braces in this case, and you should be quoting
your array keys.
For Example:
<tr>
<td class="submissionfield">Name:</td>
<td><input type="text" name="username"
size="30"/>{$errorMessage['username']}</td>
</tr>
That should solve your problem.
A better way may be be to use a loop:
echo '<form action="' . $_SERVER['PHP_SELF'] . '" method="post"
enctype="multipart/form-data">' . "\n";
$fields = array (
'username' => array (
'title' => 'Name',
'field' => '<input type="text" name="username" size="30" />'
),
'email' => array (
'title' => 'Email Address',
'field' => '<input type="text" name="email" size="30" />'
)
);
echo '<table cellpadding="5" cellspacing="5" class="submissionstable">'
. "\n";
foreach ( $fields as $field_id => $field )
{
echo '<tr><td class="submissionfield">' . $field['name'] . ':</td><td>'
. $field['field'] . ' ' . $errorMessage[$field_id] . '</td></tr>' . "\n";
}
echo '</table>' . "\n";
echo '</form>' . "\n";
This makes it very easy to add extra fields, or change format of the HTML.
Dan
Webapprentice . wrote:
> Hi,
> I'm trying to print out the contents of the $errorMessage array in this heredoc, but if I don't put the double quotes around the variable, PHP gives me an error. If I use the double quotes and the variable happens to be empty, I get "" in the page, which is not what I want.
>
> How do I fix this?
>
> Thanks,
> Stephen
>
> echo <<< UPLOADFORM
> <form action="$_SERVER[PHP_SELF]" method="post" enctype="multipart/form-data">
>
> <table cellpadding="5" cellspacing="5" class="submissionstable">
> <tr>
> <td class="submissionfield">Name:</td>
> <td><input type="text" name="username" size="30" /> "$errorMessage[username]"</td>
> </tr>
> <tr>
> <td class="submissionfield">Email Address:</td>
> <td><input type="text" name="email" size="30" /> "$errorMessage[email]"</td>
> </tr>
> <tr>
> <td class="submissionfield">Soundtrack Title:</td>
> <td><input type="text" name="title" size="30" /> "$errorMessage[title]"</td>
> </tr>
> <tr>
> <td class="submissionfield">Composed by:</td>
> <td><input type="text" name="composer" size="30" /> "$errorMessage[composer]"</td>
> </tr>
> <tr>
> <td class="submissionfield">Arranged by:</td>
> <td><input type="text" name="arranger" size="30" /> "$errorMessage[arranger]"</td>
> </tr>
> <tr>
> <td class="submissionfield">Publisher:</td>
> <td><input type="text" name="publisher" size="30" /> "$errorMessage[publisher]"</td>
> </tr>
> <tr>
> <td class="submissionfield">Catalog Number:</td>
> <td><input type="text" name="catalognumber" size="30" /> "$errorMessage[catalognumber]"</td>
> </tr>
> <tr>
> <td class="submissionfield">CD Info:</td>
> <td><input type="text" name="cdinfo" size="30" /> "$errorMessage[cdinfo]"</td>
> </tr>
> <tr>
> <td class="submissionfield">Release Date:</td>
> <td><input type="text" name="releasedate" size="30" /> "$errorMessage[releasedate]"</td>
> </tr>
> <tr>
> <td class="submissionfield">Disc Time:</td>
> <td><input type="text" name="disctime" size="30" /> "$errorMessage[disctime]"</td>
> </tr>
> <tr>
> <td class="submissionfield">Tracklist:</td>
> <td><textarea name="tracklist" rows="10" cols="50"></textarea> "$errorMessage[tracklist]"</td>
> </tr>
> <tr>
> <td class="submissionfield">Translation by:</td>
> <td><input type="text" name="translator" size="30" /> "$errorMessage[translator]"</td>
> </tr>
> <tr>
> <td class="newsitem">Front CD Cover Image (BMP, JPG, or GIF):</td>
> <td><input name="frontimage" type="file" size="60" /> "$errorMessage[frontimage]"</td>
> </tr>
> <tr>
> <td class="newsitem">Rear CD Cover Image (BMP, JPG, or GIF):</td>
> <td><input name="rearimage" type="file" size="60" /> "$errorMessage[rearimage]"</td>
> </tr>
> <tr>
> <td class="newsitem"><input type="submit" value="Submit" /></td>
> <td><input type="hidden" name="stage" value="process" /></td>
> </tr>
> </table>
> </form>
> UPLOADFORM;
More information about the talk
mailing list