Programmer's Reference Guide

概要

PHP ファイルの書式

全般

PHP コードのみからなるファイルでは、終了タグ ("?>") は決して含めてはいけません。これは必須なものではなく、 終了タグを省略することで、ファイルの最後にある空白文字が出力に影響することを防ぎます。

注意: 重要: Zend Framework の PHP ファイルやそこから派生したものの中では、 __HALT_COMPILER() を使用して任意のバイナリデータを含めることを禁じます。 この機能は、インストールスクリプトなどの特別な場合にのみ使用します。

字下げ

字下げは空白 4 文字で行います。タブ文字を使ってはいけません。

1 行の長さ

1 行の長さを 80 文字までにすることを目指しましょう。すなわち、 コードの長さを現実的な範囲で 80 文字までにおさめるよう努力すべきです。 しかしながら、場合によっては少々長くなってしまってもかまいません。 PHP コードの行の長さは、最大 120 文字までにするようにしましょう。

行末

行末の扱いは、標準的な Unix テキストファイルの方式にあわせます。 行末は、ラインフィード (LF) のみにしなければなりません。 この文字のコードは 10、あるいは 16 進形式で 0x0A となります。

Apple OS のようなキャリッジリターン (CR) (0x0D) や Windows OS のようなキャリッジリターンとラインフィードの組み合わせ (CRLF) (0x0D, 0x0A) を使用しないでください。


概要

Comments

Hier vermisse ich den Hinweis, dass das starke Vermischen von HTML und PHP schlechter Stil ist. Eine PHP Datei sollte, wenn sie überhaupt HTML erzeugt strikt zwischen PHP und HTML Teil trennen.
Zum Beispiel

<?php

includes

functions

processing

/***HTML OUTPUT****/
?>
<html>
..
<?php echo $header; ?>
<?php buildTable(...); ?>
</html>
$ grep -RP "\t" library/Zend | wc -l
232417

a lot of tabs if you ask me
This is the most informed writing I’ve ever seen on this subject. <a href="http://www.google.com"> search </a>
@tvm : Nice one;
@adam : ++;

Nice april fools Zend so fun !
Omitting the closing tag to prevent the accidental injection of trailing white space? When there is an opening tag, there is *always* a closing tag. That is how proper formatting is done. Zend is trying to solve a mere technical problem by breaking syntax. A very bad idea. And for crying out loud, how come there is a trailing white space? I have been writing php scripts for over 10 years now, since the days of php/fi. Trailing white space was never an issue for me. If it happened, I removed it. That's it. All you need is a good editor and a good ftp program. (BTW, proper editors can automatically remove trailing white spaces for you.)
"Indentation should consist of 4 spaces. Tabs are not allowed." WTF? This, again, makes no sense. This suggests misunderstanding of what certain characters are for. Spaces are *not* for cosmetic formatting of code. Tabs are generally used for indentation - in fact, that is what they were created for. If an editor messes up code indented with tabs, that is a bad editor. Good ones allow the exact setting of tab stops. This is again a very bad approach to solving an otherwise very easy problem.
Maximum Line Length and Line Termination are okay.
adam, you're right.
I strongly agree with adam. Drives me nuts when I'm going through code and I see indentations ranging from 1-4 spaces. 1 tab and you're sorted. One key to indent, one key to delete an indentation.

Spamming space and delete is stupid and its contributing to RSI!

Honestly, I cannot stress enough how much it annoys me when I see 1 space, then 2 spaces, then 3 spaces, then 2 spaces, then 4. I don't mean to but being a little obsessive I waste a lot of time trying to sort them out for readability.

Ctrl + a, open, find+replace, 4 spaces for a tab :P
@adam:
"The closing tag of a PHP block at the end of a file is optional, and in some cases omitting it is helpful when using include() or require(), so unwanted whitespace will not occur at the end of files, and you will still be able to add headers to the response later. It is also handy if you use output buffering, and would not like to see added unwanted whitespace at the end of the parts generated by the included files."
From http://php.net/manual/en/language.basic-syntax.instruction-separation.php

+ Add A Comment

Please do not report issues via comments; use the ZF Issue Tracker.

If you have a JIRA/Crowd account, we suggest you login first before commenting.

  • BBCode is allowed in the comment markup

  • Select a Version

    Languages Available

    Components

    Search the Manual