#native_company# #native_desc#

Best Practices: PHP Coding Style Page 3

By Tim Perdue
on January 3, 2001

Function Calls

Functions should be called with no space between between the function name and
the parentheses. Spaces between params and operators are encouraged.
$var = myFunction($x, $y);


Function calls are braced differently than conditional expressions. This is a
case where I’ll have to change my personal coding style in order to be kosher,
but I guess it needs to be done.
function myFunction($var1, $var2 = '')
	//indent all code inside here
	return $result;
Notice again there is no space between the function name and the parens, and that
the params are nicely spaced. All your code inside the function will be at least
4 spaces indented.
Another important principle when coding functions is that they should always
return instead of print directly. Remember, if you print directly inside
your function call, whomever calls your function cannot capture its output using a


Borrowing – almost in its entirety – from the JavaDoc spec, PEAR strongly encourages
the use of PHPDoc comment style. JavaDoc is rather
clever because you format your
code comments in such a manner that they can be parsed by a doc-generating tool,
essentially creating “self commenting” code. You can then view the resulting docs
using a web browser.
As a reformed java programmer myself, I really like this standard and am trying to
go back through my code and add PHPDoc comments.
 *	short description of function
 *	Optional more detailed description.
 *	@param $paramName - type - brief purpose
 *	@param ...
 *	...
 *	@return type and description

Including Code

PHP4 introduced a pretty useful new feature: include_once(). I’ve seen a lot
of questions on the mailing lists and discussion boards caused by people including
the same files in multiple places. This can cause conflicts when the included files
include functions, which can be defined only once per script.
The simple solution is to replace all your include() and require() calls with
corresponding include_once() and require_once(). Both use the same file list, so a
file included with require_once() will not be later included with an include_once()
call. (technically, require_once and include_once are “operators”, not “functions”
so parens are not necessary).
I never use include, or any of these *_once functions. IMHO, well-designed applications
include files in one place that is easy to maintain and keep track of. Others will disagree
and say that each included file should require_once() every file that it depends on.
This seems like extra overhead and maintenance headaches to me personally.
Either way, the days of conflicting includes are probably over.