1. For PHP files the closing tag ("?>") is to be omitted. It is not required by PHP, and omitting it prevents trailing whitespace from being accidentally injected into the output.
In fact the problem comes when you don’t use gzip or output buffering in php :
// php.ini – Turn off compress output and output buffering
zlib.output_compression = off
output_buffering = off
// foo.php – There are some white spaces after "?>"
<?php $foo = 'foo'; ?>
[/codesyntax]
// index.php – In fact we have already output some white spaces when include foo.php
<?php include 'foo.php'; session_start(); ?>
[/codesyntax]
We will see the warning saying that "… Cannot send session cache limiter – headers already sent …" because of the output of some white spaces before session_start() is called.
2. Inclusion of arbitrary binary data as permitted by __halt_compiler() is prohibited from PHP files in the Zend Framework project or files derived from them.
In fact it’s to prevent some cases unpredictable. Take the example above :
// foo.php – we have already permitted "?>"
<?php // Perhaps we want to output something by using __halt_compiler() if (defined('__COMPILER_HALT_OFFSET__')) { $fp = fopen(__FILE__, 'r'); fseek($fp, __COMPILER_HALT_OFFSET__); var_dump(stream_get_contents($fp)); } // At the end of script __halt_compiler();
[/codesyntax]
We still see the warning until __halt_compiler() is commentted. In other hand please pay attention that __halt_compiler() is language structure but not function or method.
3. Use an indent of 4 spaces with no tab characters. Editors should be configured to treat tabs as spaces in order to prevent injection of tab characters into the source code.
Its mainly goal is to keep the source code looks clean and neat Especially in different IDE or editors. In fact it had already became an default standard for software development.
For example :
if ($x == 1) {
$indented_code = 1;
if ($new_line == 1) {
$more_indented_code = 1;
}
}
[/codesyntax]
4. Multiple assignments must have the same indentation.
For example :
$variable1 = "demo";
$var2 = "demo2";
[/codesyntax]
5. The target line length is 80 characters. The maximum length of any line of PHP code is 120 characters.
Since linux read the file by 80 charactors per time which means it needs additional operations if one line with more than 80 charactors. It seems a little problem, but it’s worth to compliance especially for those strive for perfection.
For example some lines in Zend/Navigation.php :
6. Lines should not contain trailing spaces.
In order to facilitate this convention, most IDEs and editors can be configured to strip trailing spaces, such as upon a save operation.
7. Line termination is the standard way for Unix text files. Lines must end only with a linefeed "\n" (LF). Linefeeds are represented as ordinal 10, or hexadecimal 0x0A. Do not use carriage returns "\r" (CR) like Macintosh computers (0x0D). Do not use the carriage return/linefeed combination "\r\n" (CRLF) as Windows computers (0x0D, 0x0A).
It doesn’t means to belittle apple or windows. The fact is that LAMP is still mainstream and we need to follow its file formatting standard for better performance.