PHP Coding Standards
Table of Content
1. General
1.1 Opening and Closing PHP Tags
When embedding multi-line PHP snippets within an HTML block, the PHP open and close tags must be on a line by themselves.
1.2 No Shorthand PHP Tags
Incorrect
Correct
1.3 Single and Double Quotes
Always use single quote, only use double quote when required
1.4 Writing include/require statements
It is recommended to use require over include. As it throws error and bugs can be fixed earlier
2. Naming Conventions
2.1 Lowercase letter and Underscore seperated
- Variable
- Actions/Filters
- Functions
💔 Note: With the introduction of named arguments in PHP 8.0, where you can call a function by explicitly naming its parameters, renaming a function parameter is considered Breaking change.
2.2 Capitalized words separated by underscores.
- Class
- Traits
- Interfaces
- Enum
2.3 Uppercase
-
Constants
2.4 File names
-
Class name should be prepended by
class-
-
Files containing template tags in the wp-includes directory should have
-template
3. Whitespace
- Always put spaces after commas, and on both sides of logical, arithmetic, comparison, string and assignment operators.
- Spaces on both sides of the opening and closing parentheses
- Remove trailing whitespace at the end of each line.
- For associative arrays, each item should start on a new line when the array contains more than one item.
4. Formatting
4.1 Braces
Use Braces even if their is only one statement in this style
4.2 Declaring Arrays
- Use
array(1,2,3)
instead of[1,2,3]
4.3 Multiline Function Calls
- Functions having parameters which takes more than 1 line is considered Multiline.
- Multiline parameter should be broken in variables and these variable should be passed to functions