NYCPHP Meetup

NYPHP.org

[nycphp-talk] Can PHP be wraqpped around HTML??

Rob Marscher rmarscher at beaffinitive.com
Wed Jan 16 13:22:19 EST 2008


On Jan 15, 2008, at 3:56 PM, PaulCheung wrote:
> I am trying to wrap PHP coding around HTML coding and not the usual  
> other way around.

Actually... this is what php was originally designed to do (10+ years  
ago).

The other responses are all valid... but hmy favorite way of writing  
php that's enclosed by html is to use the alternative syntax for  
control structures (http://php.net/manual/en/control-structures.alternative-syntax.php 
):

<table>
	<tr>
	<?php if (update_ind != "u"): ?>
		<td>
			<a href="#" onclick="javascript: window.print()"><button>Print</ 
button></a>
			<input value="UPDATE" type="submit" />
		</td>
	<?php endif; ?>
	</tr>
<table>

I think that's the most readable way of doing it.  Often files like  
this are given a .phtml extension -- the Zend Framework defaults to  
this way of templating.  If you look at the compiled templates for  
Smarty, they also use this way of writing control structures.  It  
reminds me of Basic/VB.

If you use short tags, you can echo stuff like this:

<p><?= $myMessage ?></p>

which is nice because of how compact it is.  However, short tags don't  
play well with XML/XHTML files so I (and many other people) turn them  
off and must instead use the following:

<p><?php echo $myMessage; ?></p>

FYI... found this little snippet in the php documentation: "for  
outputting large blocks of text, dropping out of PHP parsing mode is  
generally more efficient than sending all of the text through echo()  
or print()."  http://php.net/manual/en/language.basic-syntax.php

However, those kinds of little optimizations don't usually end up  
affecting performance by much... usually there's much larger fish to  
fry -- like database queries.





More information about the talk mailing list