Excellent PHP Tips/Tricks/Techniques To Follow

Status
Not open for further replies.


To throw in my 2 cents....

A contractor im working with on a big project with told me this and ive found it the cleanest and easiest way to go....

To avoid problems and generally keep things cleaner, use PHP to put information INTO HTML, dont use PHP to create HTML....

Simply put...

Right:
<?
for($i = 0; $i <= 10; $i++) {
?>
<div>
<img src="<?= $image[$i]; ?>">
</div>
<?
}
?>

Wrong:
<?
for($i = 0; $i <= 10; $i++) {
echo "<div>
<img src=\"".$image[$i]."\">
</div>";
}
?>

Looking at that small of an example, it looks negligible to do it that way... But the ideas, as I said above, is to use PHP to put information into HTML, rather then output HTML with PHP...
 
To throw in my 2 cents....

A contractor im working with on a big project with told me this and ive found it the cleanest and easiest way to go....

To avoid problems and generally keep things cleaner, use PHP to put information INTO HTML, dont use PHP to create HTML....
This is what frameworks seek to do by forcing you to use the model/view/controller paradigm.

You can also look into the Smarty templating system.
 
To avoid problems and generally keep things cleaner, use PHP to put information INTO HTML, dont use PHP to create HTML....

Definitely. Separating logic from presentation is a good programming concept not limited to PHP/HTML.
 
Supergeek,

Yea, thats where I got it from, were working on a large scale project and using Cake, besides getting used to heavy OO and the seperation of MVC it really is a great framework and quiet powerful to boot. Were using the latest iteration 1.2 which brought some nice changes.
 
To throw in my 2 cents....

A contractor im working with on a big project with told me this and ive found it the cleanest and easiest way to go....

To avoid problems and generally keep things cleaner, use PHP to put information INTO HTML, dont use PHP to create HTML....

Simply put...

Right:
<?
for($i = 0; $i <= 10; $i++) {
?>
<div>
<img src="<?= $image[$i]; ?>">
</div>
<?
}
?>

Wrong:
<?
for($i = 0; $i <= 10; $i++) {
echo "<div>
<img src=\"".$image[$i]."\">
</div>";
}
?>

Looking at that small of an example, it looks negligible to do it that way... But the ideas, as I said above, is to use PHP to put information into HTML, rather then output HTML with PHP...


Damn fine idea. Wish that would have clicked in my idea a little sooner. Would have made a few projects aLOT easier.
 
Status
Not open for further replies.