#native_company# #native_desc#
#native_cta#

Best Practices: PHP Coding Style Page 4

By Tim Perdue
on January 3, 2001

PHP Tags

You should always use the full <?php ?> tags to enclose your code, not the
abbreviated <? ?> tags, which could conflict with XML or other languages.
ASP-style tags, while supported, are messy and discouraged.

Strings

The proper use of strings is not really discussed in the PEAR RFC, but I’m going to
cover it anyway. In PHP of course, double quotes (“) are parsed, but single quotes (‘)
are not. That means PHP does extra work (magic really) on double-quoted strings that it doesn’t
do on single-quoted strings. So there is a (subtle) performance difference between the two,
especially in code that is iterated or called a large number of times.
Best:
$associative_array['name'];
$var='My String';
$var2='Very... long... string... ' . $var . ' ...more string... ';
$sql="INSERT INTO mytable (field) VALUES ('$var')";
Acceptable:
$associative_array["name_$x"];
$var="My String $a";
The first example does not include any vars that need to be appended or parsed into the strings,
so single quotes (‘) are definitely the best way to go. The second example includes very short
strings and has variables that need to be parsed into the strings, so it is acceptable to use
double quotes. If the strings were long, it would be best to use the append (.) operator.
For consistency’s sake, I always use single quotes around all my strings, except SQL statements,
which almost always contain apostrophes and variables that must be parsed into the strings.
Well, now you’ve got the fundamentals of good coding style to work with. If you have
comments, please post them below. Not everyone will agree with the standards laid out here,
nor should they. I may respectfully disagree with certain aspects of this RFC, but I
strongly agree with the core value of it and its goals (to improve PHP coding for everyone).
–Tim

1
|
2
|
3
|
4