[FX.php List] Risks in multi-line echo statement?

Chris Hansen chris at iViking.org
Wed Oct 25 12:08:09 MDT 2006


Joel,

Use heredoc syntax.  This would like like the following:

$myText = <<<MY_TEXT
<table>
	<tr>
		<td align="right">Field A: </td>
		<td>{$value['FieldA'][0]}</td>
	</tr>
	<tr>
		<td align="right">Field B: </td>
		<td>{$value['FieldB'][0]}</td>
	</tr>
</table>
MY_TEXT;

echo($myText);

Note that like double quotes, your variables can go right inside the  
block.  Also, heredoc is designed to work with multi-line blocks of  
text.  The PHP online docs have more details:

http://www.php.net/manual/en/ 
language.types.string.php#language.types.string.syntax.heredoc

Lots of folks aren't aware of this string syntax, but it's a gem in  
my opinion.  (You may be able to echo the heredoc block directly  
rather than storing it first -- I don't use it that way though.)  HTH

--Chris Hansen
   FileMaker 7 Certified Developer
   Creator of FX.php
   "The best way from FileMaker to the Web."
   www.iViking.org


On Oct 25, 2006, at 11:51 AM, Joel Shapiro wrote:

> Hi all
>
> Quick php formatting question:
>
> Are there any risks to using a single echo statement with multiple  
> lines inside the quotes, as follows?
>
> echo ' // START ECHO
> <table>
> 	<tr>
> 		<td align="right">Field A: </td>
> 		<td>'.$value['FieldA'][0].'</td>
> 	</tr>	
> 	<tr>
> 		<td align="right">Field B: </td>
> 		<td>'.$value['FieldB'][0].'</td>
> 	</tr>
> </table>
> '; //  END ECHO
>
> I think it was on this list where people once discussed various  
> ways of switching between html and php code.  This way *seems* to  
> be a way to keep the html looking like html (both in the initial  
> code, and in the browser's view source), but without needing lots  
> of echo statements (1 per line), or using "\n", or having to use <? 
> php echo $value['FieldA'][0]; ?> each time within straight html.   
> I've looked elsewhere online and haven't found other examples of  
> this, so I'm wondering if there's a good reason for that.
>
> TIA,
> -Joel
> _______________________________________________
> FX.php_List mailing list
> FX.php_List at mail.iviking.org
> http://www.iviking.org/mailman/listinfo/fx.php_list
>



More information about the FX.php_List mailing list