tags:

views:

34

answers:

3

How do put htmlcode in heredoc syntax. With in this HTML i May have some PHP Variables needs to be interpreted and Some PHP loops needs to be executed .How Do i do all this can any one show an example

$con = <<<EOC
<style type="text/css">
.tddata {color:#330000;border-width:0px;font-family:Arial;font-size:12pt;font-weight:bold}
.tdheading {color:#FF0000;border-width:0px;font-family:Arial;font-size:12pt;font-weight:bold"}
</style>
</head>
<table border="0" width="500">
 <tr>
  <td colspan ="5" style="color:#FF0000;border-width:0px;font-family:Arial;font-size:12pt;font-weight:bold" nowrap>{ $_REQUEST ['SOMEPHP']; }</td>
  <td style="color:#FF0000;border-width:0px;font-family:Arial;font-size:12pt;font-weight:bold" nowrap>This report was created on <?php echo (date('d-M-Y')?></td>
 </tr>
</table>

<table border="1" width="700"> 
<tr>
<td class="tddata" bgcolor="#808080" colspan="1">Mode</td> 
<td class="tddata" bgcolor="#808080"><?php  echo $_POST['MODE'] ;?></td> 
<td class="tddata" bgcolor="#808080">Type</td> 
<td class="tddata" bgcolor="#808080" colspan="1"><?php  echo $_POST['TYPE'] ;?></td> 
</tr>
EOC
+3  A: 

For the variables, just use the same syntax that you would for double-quoted strings. For the loops, you'll need to break it into multiple heredocs with PHP code in-between each.

Ignacio Vazquez-Abrams
+4  A: 

Loops can not be done in a HEREDOC. However variables are interpreted just as they would be within double quotes. Check out the manual.

With that said. If you want to output a bunch of code - variables, tags, and loops - I'd suggest looking at output buffering.

Or simply just have your HTML outside your PHP blocks.

Jason McCreary
+1 for the output buffering remark. Using ob_start, ob_get_clean and standard <?php ... ?> syntax is a simpler alternative to what you're trying to do.
Victor Nicollet
@Victor Thanks. Although nice, I rarely use `heredoc` and feel that seems to be the consensus for the PHP community. At least judging by large scale open source projects - WordPress, Magento, CakePHP, etc.
Jason McCreary
+3  A: 

Remeber no space between { and $ when doing variable substitution. And the HEREDOC terminator (EOC in your case) must start on the first column and end with a semicolon.

Try this:

$date = date('d-M-Y');
$con = <<<EOC
  <style>...</style>
  </head>
  <table border="0" width="500">
  <tr>
    <td colspan ="5" style="..." nowrap>{$_REQUEST['SOMEPHP']}</td>
    <td style="..." nowrap>This report was created on {$date}</td>
  </tr>
  </table>

  <table border="1" width="700"> 
  <tr>
    <td class="tddata" bgcolor="#808080" colspan="1">Mode</td> 
    <td class="tddata" bgcolor="#808080">{$_POST['MODE']}</td> 
    <td class="tddata" bgcolor="#808080">Type</td> 
    <td class="tddata" bgcolor="#808080" colspan="1">{$_POST['TYPE']}</td> 
  </tr>
EOC;

// loop example
for ($i=0; $i < 10; $i++) {
    $con .= <<<EOC
        <p>{$i}</p>
EOC;
gregjor
agree with above that if you are making big HEREDOCs in your PHP it might be better to just switch in/out of PHP mode instead.
gregjor
@Someone as noted by **gregjor**, this arguably isn't the *best* way to do this. Although this is the answer to this specific question, I don't recommend you write your PHP like this.
Jason McCreary