tags:

views:

34

answers:

6

I've been trying the heredoc method like this:

<?php  echo $form = <<<HTML ?>  

here follows the html...
<form method="post" >
<table border="0" cellpadding="0"  cellspacing="0" id="reserv_table">
<tr>
<td><span id="message"><b>for you : 9€</b> &nbsp;</span></td>
<td><input type="text" name="_membre" id="_membre" style="width: 40px;" class="text disabled" disabled="disabled" /></td>
<td valign="top">
<table>
<tr> 


<?php HTML; ?> 

But I get an error.

A: 

Why not to try heredoc according to it's syntax rules?

though to print out this form, you need no heredoc.
just print it out as is

here follows the html...
<form method="post" >
<table border="0" cellpadding="0"  cellspacing="0" id="reserv_table">
<tr>
<td><span id="message"><b>for you : 9€</b> &nbsp;</span></td>
<td><input type="text" name="_membre" id="_membre" style="width: 40px;" class="text disabled" disabled="disabled" /></td>
<td valign="top">
<table>
<tr> 
Col. Shrapnel
+3  A: 

If you're just echoing the form you don't even need PHP's heredoc syntax. Just break out of PHP using the closing delimiter, then return to PHP once you're done outputting the HTML form.

EDIT: OK, so you need the form output to be stored in a variable, but you also want it to be printed to the browser? No problem, use output buffering (in particular, ob_start() and ob_get_flush():

<?php

// Begin output buffering
ob_start();

// Break out of PHP...

?>

<form method="post" >
<table border="0" cellpadding="0"  cellspacing="0" id="reserv_table">
<tr>
<td><span id="message"><b>for you : 9€</b> &nbsp;</span></td>
<td><input type="text" name="_membre" id="_membre" style="width: 40px;" class="text disabled" disabled="disabled" /></td>
<td valign="top">
<table>
<tr> 

<?php

// Back to PHP, store the HTML in $form AND print it to the browser
$form = ob_get_flush();

?>
BoltClock
make SO highlight HTML is easy ;)
Col. Shrapnel
@Col Shrapnel: ha! That slipped my mind, thanks ;)
BoltClock
+1 for the proper solution but it answers wrong question. One should avoid such a variables
Col. Shrapnel
A: 

Why don't you just remove the php code like this

<form method="post" >
<table border="0" cellpadding="0"  cellspacing="0" id="reserv_table">
<tr>
<td><span id="message"><b>for you : 9€</b> &nbsp;</span></td>
<td><input type="text" name="_membre" id="_membre" style="width: 40px;" class="text disabled" disabled="disabled" /></td>
<td valign="top">
<table>
<tr>

Mean no echo is needed.

Bang Dao
+2  A: 

Your PHP tags <?php and ?> are messing with your Heredoc tags.

  • Remove the ?> at the end of the first line
  • Remove the <?php and the space at the start of the last line
  • Remove the space and the ?> at the end of the last line and place it one line below

This is all assuming that you need all the code to be placed into the $form variable for some reason. If you don't, pick BoltClock's answer.

If after fixing your code, your editor won't syntax highlight it properly, then your editor is at fault. You could either replace it with a different editor, or just avoid using Heredoc syntax. I tend to avoid using Heredoc but mainly because I think it makes things messy.

thomasrutter
Yes i need the form in a variable
tada
I did that but the editor syntax highlight gets broken, how can i make the same thing but without heredoc ?, my editor is at fault here
tada
@tada: I updated my answer.
BoltClock
@tada in that case BoltClock's updated answer is one way. Another way would be to just use it as a normal string with single quotes around it. You can have a normal string that spans more than one line.
thomasrutter
A: 

If you need to send static data you can do it putting it directly in the code outside the PHP tags

ex:

   <?php 
     echo "PHP Code";
   ?>
     HTML code

   <?php
     echo "PHP Code again";
   ?>

Also if you are triying to create a PHP String you need to keep all the content in your PHP Code

ex:

   <?php 
     echo "PHP Code";
     $heredoc_variable = <<<HTMLCODE
       <b> This is html code into a heredoc variable in php </b>
     HTMLCODE;
   ?>
Dubas
A: 

If you really need it to be a variable and are looking for a really simple method, just include the entire form syntax in a variable with escaped quotes like so:

<?php

$output = "
<form method=\"post\" >
<table border=\"0\" cellpadding=\"0\"  cellspacing=\"0\" id=\"reserv_table\">
<tr>
<td><span id=\"message\"><b>for you : 9€</b> &nbsp;</span></td>
<td><input type=\"text\" name=\"_membre\" id=\"_membre\" style=\"width: 40px;\" class=\"text disabled\" disabled=\"disabled\" /></td>
<td valign=\"top\">
<table>
<tr>
";

echo $output; //or wherever you need to echo it...

?>

This method is a simple solution that is bound to use less resources than ob_start(); in my opinion.

webfac
It also look absolutely hideous and requires you to escape every single quote. That's why HEREDOCs are available, to dump multiline text into a variable WITHOUT having to escape quotes.
Marc B
@Marc B - It's not a beauty contest, and besides one could enclose everything in single quotes then there would be no need to escape the double quotes. It's personal preference I guess. I hear what you're saying though, and I do not disagree with you at all.
webfac