**Bad** (the total length of the line is **340** characters - from [Lorem Ipsum](https://www.lipsum.com/feed/html))
```PHP
$longtext = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse condimentum nec est eget posuere. Proin at sagittis risus. Fusce faucibus lectus leo, eu ornare velit tristique eu. Curabitur elementum facilisis ultricies. Praesent dictum fermentum lectus a rhoncus. Donec vitae justo metus. Sed molestie faucibus egestas.';
```
**Good** (use `heredoc` syntax - this will add line-breaks)
```PHP
$longtext = <<
_Reference_: [`Generic.Files.LineLength`](https://github.com/squizlabs/PHP_CodeSniffer/blob/master/src/Standards/Generic/Sniffs/Files/LineLengthSniff.php)
# Strings
## Whenever possible use single quote strings
PHP supports both single quote strings and double quote strings. For pure text you must use single quote strings for consistency. Double quote strings are only allowed for special characters (i.e. `"\n"`) or inlined variables (i.e. `"My name is {$name}"`);
Example
**Bad**
```PHP
echo "Hello World!";
```
**Good**
```PHP
echo 'Hello World!';
```
_Reference_: [`Squiz.Strings.DoubleQuoteUsage`](https://github.com/squizlabs/PHP_CodeSniffer/blob/master/src/Standards/Squiz/Sniffs/Strings/DoubleQuoteUsageSniff.php)
## Add spaces around the concatenation operator
The concatenation operator should have one space on both sides in order to improve readability.
Example
**Bad**
```PHP
$text = $greeting.' '.$name.'!';
```
**Good** (add spaces)
```PHP
$text = $greeting . ' ' . $name . '!';
```
You may break long lines into multiple lines using the concatenation operator. That way readability can improve considerable when combining lots of variables.
Example
**Bad**
```PHP
$text = $greeting.' '.$name.'!';
```
**Good** (split into multiple lines)
```PHP
$text = $greeting
. ' '
. $name
. '!';
```
_Reference_: [`Squiz.Strings.ConcatenationSpacing`](https://github.com/squizlabs/PHP_CodeSniffer/blob/master/src/Standards/Squiz/Sniffs/Strings/ConcatenationSpacingSniff.php)
## Use a single string instead of concatenating
While concatenation is useful for combining variables with other variables or static text. It should not be used to combine two sets of static text. See also: [Maximum line length](#maximum-line-length)
Example
**Bad**
```PHP
$text = 'This is' . 'a bad idea!';
```
**Good**
```PHP
$text = 'This is a good idea!';
```
_Reference_: [`Generic.Strings.UnnecessaryStringConcat`](https://github.com/squizlabs/PHP_CodeSniffer/blob/master/src/Standards/Generic/Sniffs/Strings/UnnecessaryStringConcatSniff.php)
# Constants
## Use UPPERCASE for constants
As in most languages, constants should be written in UPPERCASE.
_Notice_: This does not apply to keywords!
Example
**Bad**
```PHP
const pi = 3.14;
```
**Good**
```PHP
const PI = 3.14;
```
_Reference_: [`Generic.NamingConventions.UpperCaseConstantName`](https://github.com/squizlabs/PHP_CodeSniffer/blob/master/src/Standards/Generic/Sniffs/NamingConventions/UpperCaseConstantNameSniff.php)
# Keywords
## Use lowercase for `true`, `false` and `null`
`true`, `false` and `null` must be written in lower case letters.
Example
**Bad**
```PHP
if($condition === TRUE && $error === FALSE) {
return NULL;
}
```
**Good**
```PHP
if($condition === true && $error === false) {
return null;
}
```
_Reference_: [`Generic.PHP.LowerCaseConstant`](https://github.com/squizlabs/PHP_CodeSniffer/blob/master/src/Standards/Generic/Sniffs/PHP/LowerCaseConstantSniff.php)
# Operators
## Operators must have a space around them
Operators must be readable and therefore should have spaces around them.
Example
**Bad**
```PHP
$text='Hello '.$name.'!';
```
**Good**
```PHP
$text = 'Hello ' . $name . '!';
```
_Reference_: [`Squiz.WhiteSpace.OperatorSpacing`](https://github.com/squizlabs/PHP_CodeSniffer/blob/master/src/Standards/Squiz/Sniffs/WhiteSpace/OperatorSpacingSniff.php)
# Functions
## Parameters with default values must appear last in functions
It is considered good practice to make parameters with default values last in function declarations.
Example
**Bad**
```PHP
function showTitle($duration = 60000, $title) { ... }
```
**Good**
```PHP
function showTitle($title, $duration = 60000) { ... }
```
_Reference_: [`PEAR.Functions.ValidDefaultValue`](https://github.com/squizlabs/PHP_CodeSniffer/blob/master/src/Standards/PEAR/Sniffs/Functions/ValidDefaultValueSniff.php)
## Calling functions
Function calls must follow a few rules in order to maintain readability throughout the project:
**Do not add whitespace before the opening parenthesis**
Example
**Bad**
```PHP
$result = my_function ($param);
```
**Good**
```PHP
$result = my_function($param);
```
**Do not add whitespace after the opening parenthesis**
Example
**Bad**
```PHP
$result = my_function( $param);
```
**Good**
```PHP
$result = my_function($param);
```
**Do not add a space before the closing parenthesis**
Example
**Bad**
```PHP
$result = my_function($param );
```
**Good**
```PHP
$result = my_function($param);
```
**Do not add a space before a comma**
Example
**Bad**
```PHP
$result = my_function($param1 ,$param2);
```
**Good**
```PHP
$result = my_function($param1, $param2);
```
**Add a space after a comma**
Example
**Bad**
```PHP
$result = my_function($param1,$param2);
```
**Good**
```PHP
$result = my_function($param1, $param2);
```
_Reference_: [`Generic.Functions.FunctionCallArgumentSpacing`](https://github.com/squizlabs/PHP_CodeSniffer/blob/master/src/Standards/Generic/Sniffs/Functions/FunctionCallArgumentSpacingSniff.php)
## Do not add spaces after opening or before closing bracket
Parenthesis must tightly enclose parameters.
Example
**Bad**
```PHP
if( $condition ) { ... }
```
**Good**
```PHP
if($condition) { ... }
```
_Reference_: [`PSR2.ControlStructures.ControlStructureSpacing`](https://github.com/squizlabs/PHP_CodeSniffer/blob/master/src/Standards/PSR2/Sniffs/ControlStructures/ControlStructureSpacingSniff.php)
# Structures
## Structures must always be formatted as multi-line blocks
A structure should always be treated as if it contains a multi-line block.
**Add a space after closing parenthesis**
Example
**Bad**
```PHP
if($condition){
...
}
```
**Good**
```PHP
if($condition) {
...
}
```
**Add body into new line**
Example
**Bad**
```PHP
if($condition){ ... }
```
**Good**
```PHP
if($condition) {
...
}
```
**Close body in new line**
Example
**Bad**
```PHP
if($condition){
... }
```
**Good**
```PHP
if($condition) {
...
}
```
_Reference_: [`Squiz.ControlStructures.ControlSignature`](https://github.com/squizlabs/PHP_CodeSniffer/blob/master/src/Standards/Squiz/Sniffs/ControlStructures/ControlSignatureSniff.php)
# If-Statements
## Use `elseif` instead of `else if`
For sake of consistency `else if` is considered bad practice.
Example
**Bad**
```PHP
if($conditionA) {
} else if($conditionB) {
}
```
**Good**
```PHP
if($conditionA) {
} elseif($conditionB) {
}
```
_Reference_: [`PSR2.ControlStructures.ElseIfDeclaration`](https://github.com/squizlabs/PHP_CodeSniffer/blob/master/src/Standards/PSR2/Sniffs/ControlStructures/ElseIfDeclarationSniff.php)
## Do not write empty statements
Empty statements are considered bad practice and must be avoided.
Example
**Bad**
```PHP
if($condition) {
// empty statement
} else {
// do something here
}
```
**Good** (invert condition)
```PHP
if(!$condition) {
// do something
}
```
_Reference_: [`Generic.CodeAnalysis.EmptyStatement`](https://github.com/squizlabs/PHP_CodeSniffer/blob/master/src/Standards/Generic/Sniffs/CodeAnalysis/EmptyStatementSniff.php)
## Do not write unconditional if-statements
If-statements without conditions are considered bad practice and must be avoided.
Example
```PHP
if(true) {
}
```
_Reference_: [`Generic.CodeAnalysis.UnconditionalIfStatement`](https://github.com/squizlabs/PHP_CodeSniffer/blob/master/src/Standards/Generic/Sniffs/CodeAnalysis/UnconditionalIfStatementSniff.php)
# Classes
## Use PascalCase for class names
Class names must be written in [PascalCase](http://wiki.c2.com/?PascalCase).
Example
**Bad**
```PHP
class mySUPERclass { ... }
```
**Good**
```PHP
class MySuperClass { ... }
```
_Reference_: [`PEAR.NamingConventions.ValidClassName`](https://github.com/squizlabs/PHP_CodeSniffer/blob/master/src/Standards/PEAR/Sniffs/NamingConventions/ValidClassNameSniff.php)
## Do not use final statements inside final classes
Final classes cannot be extended, so it doesn't make sense to add the final keyword to class members.
Example
**Bad**
```PHP
final class MyClass {
final public function MyFunction() {
}
}
```
**Good** (remove the final keyword from class members)
```PHP
final class MyClass {
public function MyFunction() {
}
}
```
_Reference_: [`Generic.CodeAnalysis.UnnecessaryFinalModifier`](https://github.com/squizlabs/PHP_CodeSniffer/blob/master/src/Standards/Generic/Sniffs/CodeAnalysis/UnnecessaryFinalModifierSniff.php)
## Do not override methods to call their parent
It doesn't make sense to override a method only to call their parent. When overriding methods, make sure to add some functionality to it.
Example
**Bad**
```PHP
class MyClass extends BaseClass {
public function BaseFunction() {
parent::BaseFunction();
}
}
```
**Good** (don't override the function)
```PHP
class MyClass extends BaseClass {
}
```
_Reference_: [`Generic.CodeAnalysis.UselessOverridingMethod`](https://github.com/squizlabs/PHP_CodeSniffer/blob/master/src/Standards/Generic/Sniffs/CodeAnalysis/UselessOverridingMethodSniff.php)
## abstract and final declarations MUST precede the visibility declaration
When declaring `abstract` and `final` functions, the visibility (scope) must follow after `abstract` or `final`.
Example
**Bad**
```PHP
class MyClass extends BaseClass {
public abstract function AbstractFunction() { }
public final function FinalFunction() { }
}
```
**Good** (`abstract` and `final` before `public`)
```PHP
class MyClass extends BaseClass {
abstract public function AbstractFunction() { }
final public function FinalFunction() { }
}
```
_Reference_: [`PSR2.Methods.MethodDeclaration`](https://github.com/squizlabs/PHP_CodeSniffer/blob/master/src/Standards/PSR2/Sniffs/Methods/MethodDeclarationSniff.php)
## static declaration MUST come after the visibility declaration
The `static` keyword must come after the visibility (scope) parameter.
Example
**Bad**
```PHP
class MyClass extends BaseClass {
static public function StaticFunction() { }
}
```
**Good** (`static` after `public`)
```PHP
class MyClass extends BaseClass {
public static function StaticFunction() { }
}
```
_Reference_: [`PSR2.Methods.MethodDeclaration`](https://github.com/squizlabs/PHP_CodeSniffer/blob/master/src/Standards/PSR2/Sniffs/Methods/MethodDeclarationSniff.php)
# Casting
## Do not add spaces when casting
The casting type should be put into parenthesis without spaces.
Example
**Bad**
```PHP
$text = ( string )$number;
```
**Good**
```PHP
$text = (string)$number;
```
_Reference_: [`Squiz.WhiteSpace.CastSpacing`](https://github.com/squizlabs/PHP_CodeSniffer/blob/master/src/Standards/Squiz/Sniffs/WhiteSpace/CastSpacingSniff.php)
# Arrays
## Always use the long array syntax
Arrays should be initialized using the long array syntax.
Example
**Bad**
```PHP
$data = [ 'hello' => 'world' ];
```
**Good**
```PHP
$data = array('hello' => 'world');
```
_Reference_: [`Generic.Arrays.DisallowShortArraySyntax`](https://github.com/squizlabs/PHP_CodeSniffer/blob/master/src/Standards/Generic/Sniffs/Arrays/DisallowShortArraySyntaxSniff.php)