[thelist] PHP echoing...

David U. davidu at everydns.net
Sun Aug 18 14:10:01 CDT 2002


Donald Noble wrote:
> Hi evolters,
>
> I have been usingf PHP for a whileand was wondering what the
> best/fastest way to output text is/ I assume is is dependant on the
> particular situation, but any thoughts would be helpful.
>

1) I've never found the <? code ?> text <? code ?> method to be all that
much faster. And I find it makes code quite ugly to read.  I use the <?=
syntax in small templates quite often.
example:
file1 >> $foo = 'bar'; include('file2');
file2 >> <?=$foo;?>

2) single quotes are much faster than double quotes and I have huge scripts
which show a noticable difference.
example:
file1 >>print 'foo '.$bar.' baz';
is faster than
file1 >>print "foo $bar baz";

3) For long HTML dumps in php I often either use a template system but if
it's just a one time thing I use the <<< syntax which isn't meant for speed
but makes code easy to read.
example:
print <<<EOF
<html>
    <body>
        <strong>blah</strong>
    </body>
</html>
EOF;

4) This is offtopic but for people who don't know -- The ternary operator is
a HUGE save in terms of time and code readability.  It offers no speed
increase however. (it isn't slower though)
example:
if ($foo) {
    foo_is_true($foo);
} else {
    foo_is_false($foo);
}
TURNS INTO:
(($foo) ? foo_is_true($foo) : foo_is_false($foo));

5) Last offtopic comment. -- If you are ever planning on making an
application support multiple languages it is a good idea to begin writing
strings as using the gettext() methods.  This will allow you to change the
applications lanaguage at anytime without rewriting your code.  It does
offer a speed hit though, so decide if you need it.
example:
file1 >> print _('my text'); // gettext() shorthand
INSTEAD OF
file1 >> print ('my text'); // inflexible but fast way


Hope that helps and then some,
davidu
-davidu





More information about the thelist mailing list