Home arrow PHP arrow Page 2 - JV’s Power Tips for PHP (1)
PHP

JV’s Power Tips for PHP (1)


In this article Justin provides some tips for improving our PHP applications such as a very simple but effective template technique.

Author Info:
By: Justin Vincent
Rating: 3 stars3 stars3 stars3 stars3 stars / 3
January 29, 2003
TABLE OF CONTENTS:
  1. · JV’s Power Tips for PHP (1)
  2. · Never Breaking Out of PHP
  3. · Basic Template Theory
  4. · Refining the Previous
  5. · Accessing Other Variables from Within this Type of Template
  6. · Using Objects to Store Multiple Output Values
  7. · Conclusion

print this article
SEARCH DEVARTICLES

TOOLS YOU CAN USE

advertisement
JV’s Power Tips for PHP (1) - Never Breaking Out of PHP
(Page 2 of 7 )

Hmm. What other useful tips can I think of for you?

Lets see…. Ah. I know…. Don’t break out of PHP tags at any point (period). This is a practise I carry out religiously and here’s why:

Oh how awful:

<?php $php_tags = “PHP Tags” ?>
Combining HTML output and
<?php echo $php_tags; ?>
tags really wastes my time.


Oh what beauty:

<?php
  $php_tags = “PHP Tags”;
  echo “Never breaking out of $php_tags is much less irritating.”;
?>


You should probably know... the advice I have just given is highly controversial. There are programmers out there reading this who are foaming at the mouth, jumping up and down, stomping on the ground and yelling ‘that’s not right!’.

That’s because PHP takes slightly longer to execute when working this way, but when I say ‘slightly’ I do mean ‘slightly’. We’re talking milliseconds. Even with complex scripts. A user on a 56k modem or even a T1 on the other side of the world will not notice the extra execution time when data transfer is taken into consideration. Yet there are ‘many’ benefits to working this way, some of which I will outline within this article.

If the truth be known every PHP site that I have ever written (including many that are highly complex and generate millions of hits) have been created using this style of coding. The bottleneck (if there has ever been one) has always been the ‘database server’ never the PHP. So lets not start a flame war over this. Please recognize that this is my opinion and all are free to take or leave it as they wish.

Now that I have that out of the way let us continue.

Working this way can bring up other (more technical) issues. Like what do you do when you want to output big blocks of HTML without having to put a \ behind all the “’s? Here’s how:

<?php

$turkeys = “variables”;

print <<<END
  It is nice to know that “you” can print any amount of text on
  <font color=”red”>multiple</font> lines with <b>any</b>
  character and still include $turkeys.
END;

?>


By the way, within this article I will be jumping from one way of doing things to another. Usually I will show you what I consider to be the most understandable (not necessarily the best) way of doing something and then refine it into code that is more elegant (yet not so easy to understand). It might take two or three steps to get from understandable to elegant, but you can rest assured that each step will help to broaden your understanding of PHP and PHP techniques (unless you’re a Turkey).

blog comments powered by Disqus
PHP ARTICLES

- Removing Singletons in PHP
- Singletons in PHP
- Implement Facebook Javascript SDK with PHP
- Making Usage Statistics in PHP
- Installing PHP under Windows: Further Config...
- File Version Management in PHP
- Statistical View of Data in a Clustered Bar ...
- Creating a Multi-File Upload Script in PHP
- Executing Microsoft SQL Server Stored Proced...
- Code 10x More Efficiently Using Data Access ...
- A Few Tips for Speeding Up PHP Code
- The Modular Web Page
- Quick E-Commerce with PHP and PayPal
- Regression Testing With JMeter
- Building an Iterator with PHP

Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 



© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 8 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials