[FX.php List] Embed php into HTML or vice versa?

Steve Hannah shannah at sfu.ca
Thu Aug 31 19:15:06 MDT 2006


If you are not using Smarty (or some other templating language), I  
contend that there are times when it is better to embed HTML in echo  
statements.  When you are working on larger projects this drastically  
improves readability and maintainability.   There is no absolute rule  
when echos are preferred to embedding PHP in HTML, but in general I  
will used the method that results in easier to maintain code with  
less chances of errors.  When there is a lot of complex logic (e.g.  
loops and if statements), it is generally easier to use echo  
statements with embedded HTML.  Otherwise the approach mentioned by  
Kevin (embedding PHP in HTML) is preferred.

In the case that you are embedding HTML in PHP echo statements, the  
notation mentioned below is helpful.

Best regards

Steve
On 31-Aug-06, at 5:19 PM, Kevin Futter wrote:

> On 1/9/06 10:06 AM, "Steve Hannah" <shannah at sfu.ca> wrote:
>
>> Hi Jonathan,
>>
>> One tip for making HTML-in-PHP a little easier to work with is:
>>
>> instead of
>> echo "<td>".$value['StuList'][0]."</td>";
>>
>> do
>>
>> echo "<td>{$value['StuList'][0]}</td>";
>>
>> That way you don't have to worry about forgetting a pesky
>> concatenation operator.
>> You can only do this inside double quotes, not single quotes, but it
>> works for just about any valid PHP expression.
>>
>> Best regards
>>
>> Steve
>>
>
> I'd actually handle it like this:
>
> <td><?php echo $value['StuList'][0]; ?><td>
>
> This way, the HTML is HTML, the PHP is PHP. No need to contatenate,  
> or worry
> about escaping quotes or other characters. This will work inside a  
> foreach
> loop as you write out the data too. As I've said in earlier posts,  
> doing it
> this way isn't always practical, but it's cleaner and more in the  
> spirit of
> separating the two.
>
> -- 
> Kevin Futter
> Webmaster, St. Bernard's College
> http://www.sbc.melb.catholic.edu.au/
>
>
>
> ------------------------------------------
> This e-mail and any attachments may be confidential.  You must not  
> disclose or use the information in this e-mail if you are not the  
> intended recipient.  If you have received this e-mail in error,  
> please notify us immediately and delete the e-mail and all copies.   
> The College does not guarantee that this e-mail is virus or error  
> free.  The attached files are provided and may only be used on the  
> basis that the user assumes all responsibility for any loss, damage  
> or consequence resulting directly or indirectly from the use of the  
> attached files, whether caused by the negligence of the sender or  
> not.  The content and opinions in this e-mail are not necessarily  
> those of the College.
>
>
> _______________________________________________
> 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