I’ve always been one to write clean and readable code. With that said, I’ve not been able to find out whether or not there’s any difference in processing speed when using more or less <?php tags. Here’s some code for comparison:
PREFERRED CODE:
<?php while($condition): ?>
<?php if($anotherCondition): ?>
Hello world!
<?php endif; ?>
<?php endwhile; ?>
SHORTER CODE:
<?php while($condition):
if($anotherCondition): ?>
Hello world!
<?php endif;
endwhile; ?>
You see in the second code I use only two <?php tags, but I find the first code block much easier to follow. Is there any loss in efficiency because I used more <?php tags?
There is a performance implication but it is likely negligible. Any space or tab that is not inside a
<?php ?>block will be sent as part of the output of the PHP script. To understand this consider this simple example:The PHP code:
Gets sent across the wire as
While this PHP:
Returns this:
This is not the kind of thing you really want to worry about though, I say go for readable code. I will say however that your first example involves a lot of extra typing, are you sure it’s better?