Summary
-
PHP file must start with at the end.
-
Class must be camel-cases such as StudentModel, StudentController, not use plural such as StudentsController
-
The constant in class must be all uppercase, word separated by underscore, such as:STATUS_ACTIVE
-
Function must be camel-cases starting with lowercase, such as camelCase, getData
-
The file format must be utf-8 without BOM (distinguish ANSI on windows, which is not recognized by other systems)
-
The indent must be 4 Space instead of Tab (Tab may be 8 characters length in linux vim)
-
A blank line must be inserted after namespace declaration statement block.
-
The ({}) of class and function must be in a single line. The attribute and function of class must be with one of private, protected, public. abstract and final must be the frontend of them. The static should be after them.
There must be a space character after the keyword of the control structure, but not when calling a method or function. The start curly bracket ({) of the control structure must be written on the same line as the declaration, and the end curly bracket (}) must be written on the same line after the body.
summary:
- A blank after keywords
- No blank after (
- No blank before )
- A blank between ) and {
- Indent before content.
- } should in a single line at last
<?php
//if esleif esle
if ($expr1) {
// if body
} elseif ($expr2) {
// elseif body
} else {
// else body;
}
Note:use elseif
instead of else if
, so the keyword only one word.
//switch
<?php
switch ($expr) {
case 0:
echo 'First case, with a break';
break;
case 1:
echo 'Second case, which falls through';
// no break
case 2:
case 3:
case 4:
echo 'Third case, return instead of break';
return;
default:
echo 'Default case';
}
//while and do while
<?php
while ($expr) {
// structure body
}
do {
// structure body;
} while ($expr);
//for and foreach
<?php
for ($i = 0; $i < 10; $i++) {
// for body
}
//Note: Check the array is empty or not befor foreach
foreach ($iterable as $key => $value) {
// foreach body
}
//try and catch
<?php
try {
// try body
} catch (FirstExceptionType $e) {
// catch body
} catch (OtherExceptionType $e) {
// catch body
}
Example:
<?php
namespace Vendor\Package;
use FooInterface;
use BarClass as Bar;
use OtherVendor\OtherPackage\BazClass;
class Foo extends Bar implements FooInterface
{
public function sampleFunction($a, $b = null)
{
if ($a === $b) {
bar();
} elseif ($a > $b) {
$foo->bar($arg1);
} else {
BazClass::bar($arg2, $arg3);
}
}
final public static function bar()
{
// method body
}
}
-
PHP all keywords use lowercase. eg: true, false, null
-
closure
When declaring a closure, there must be a space before and after the keyword function
and use
For example:
<?php
$closureWithArgs = function ($arg1, $arg2) {
// body
};
$closureWithArgsAndVars = function ($arg1, $arg2) use ($var1, $var2) {
// body
};