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).
Next: Basic Template Theory >>
More PHP Articles
More By Justin Vincent