Coding Conventions (PHP): Difference between revisions
No edit summary |
|||
(One intermediate revision by the same user not shown) | |||
Line 1: | Line 1: | ||
== Sublime Text Snippets == | == Sublime Text Snippets == | ||
Modified Sublime Text snippets to follow the coding style: [[File: | Modified Sublime Text snippets to follow the coding style: [[File:Snippets-PHP.7z]] | ||
Copy them to <code>~/Library/Application Support/Sublime Text | Copy them to <code>~/Library/Application Support/Sublime Text 3/Packages/PHP</code> and you are done. | ||
== Indenting and Whitespace == | == Indenting and Whitespace == | ||
Line 109: | Line 109: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
As displayed above, there should be one space on either side of an equals sign used to assign the return value of a function to a variable. In the case of a block of related assignments, more | As displayed above, there should be one space on either side of an equals sign used to assign the return value of a function to a variable. In the case of a block of related assignments, more spaces may be inserted to promote readability: | ||
<syntaxhighlight lang="php"> | <syntaxhighlight lang="php"> |
Latest revision as of 04:25, 11 February 2020
Sublime Text Snippets
Modified Sublime Text snippets to follow the coding style: File:Snippets-PHP.7z
Copy them to ~/Library/Application Support/Sublime Text 3/Packages/PHP
and you are done.
Indenting and Whitespace
Use an indent of 1 Tab ↹, no spaces.
Lines should have no trailing whitespace at the end.
Files should be formatted with \n
as the line ending (Unix line endings), not \r\n
(Windows line endings).
All text files should end in a single newline (\n
). This avoids the verbose "\ No newline at end of file" patch warning and makes patches easier to read since it's clearer what is being changed when lines are added to the end of a file.
Operators
All binary operators (operators that come between two values), such as +, -, =, !=, ==, >, etc.
should have a space before and after the operator, for readability. For example, an assignment should be formatted as $foo = $bar
; rather than $foo=$bar;
. Unary operators (operators that operate on only one value), such as ++
, should not have a space between the operator and the variable or number they are operating on.
Casting
Put a space between the (type) and the $variable in a cast: (int) $mynumber
.
Naming convention for variables and functions
- preprocessor macros/constants: name in upper case
- typedefs: name usually all upper case
- constants: upper case
- globals: first letter lower case
g_
- session: first letter lower case
s_
- cookie: first letter lower case
c_
- locals: camel case or separated by
_
e.g.:longVar
orlong_var
- functions: camel case (first letter lower case)
Code layout
If you remember nothing else, remember this: thou shalt not use K&R style braces. K&R style braces is code that has the open curly brace on the same line as the preceding C control structure.
if (condition1 || condition2) { // bad style
action1; // bad style
} // bad style
if (condition1 || condition2) // good style
{ // good style
action1; // good style
} // good style
Control Structures
Control structures include if, for, while, switch, etc
. Here is a sample if statement, since it is the most complicated of them:
if (condition1 || condition2)
{
action1;
}
elseif (condition3 && condition4)
{
action2;
}
else
{
defaultaction;
}
Control statements should have one space between the control keyword and opening parenthesis, to distinguish them from function calls.
Always use curly braces even in situations where they are technically optional. Having them increases readability and decreases the likelihood of logic errors being introduced when new lines are added.
For switch statements:
switch (condition)
{
case 1:
action1;
break;
case 2:
action2;
break;
default:
defaultaction;
}
For do-while statements:
do
{
actions;
} while ($condition);
Function Calls
Functions should be called with no spaces between the function name, the opening parenthesis, and the first parameter; spaces between commas and each parameter, and no space between the last parameter, the closing parenthesis, and the semicolon. Here's an example:
$var = foo($bar, $baz, $quux);
As displayed above, there should be one space on either side of an equals sign used to assign the return value of a function to a variable. In the case of a block of related assignments, more spaces may be inserted to promote readability:
$short = foo($bar);
$longVariable = foo($baz);