If you want to be an excellent programmer, good code is important, how can you write good code? In this article we will give you some PHP coding specifications and recommendations.
Coding specifications
<?php//start//Not end
Example sublime, setting increased, "show_encoding": True
Example, sublime "Word_wrap": "true", "wrap_width": 80,
Example, sublime "Tab_size": 4,
The PHP code should only define classes (trait)/functions/constants/Other actions that can have side effects (such as generating file output and modifying. ini configuration files, etc.), only one option.
Example, A.phpclass a{}b.phpfunction demo () {}c.phpdefine (' a ', value);d. Phpini_set (' Some_vars ', value);
Class studlycaps{}trait Studlycaps{}interface studlycaps{}
Define (' Foo_bar ', ' something More '); const Foo_bar = value;
Class studlycaps{public function StudlyCaps () { //coding ... }}
function Snake_case () { //coding ...}
Class studlycaps{ Private Function _studlycaps () { //coding ... }}
Class studlycaps{public function dosomething () { //coding ... }}
Class studlycaps{public function dosomething () { $someVariable = ' demo '; Coding ... }}
Class studlycaps{public function dosomething ($variableOne, $variableTwo) { //coding ... }}
$a = $b + $c; $a = $b. $c;
namespace standard;//empty line use Test\testclass;//use introduce class//empty line
Class studlycaps{}
Class studlycaps{public function StudlyCaps () { //coding ... }} function Snake_case () { //coding ...}
The properties and methods of the class must add access modifiers (private, protected, and public), abstract and final must be declared before the access modifier, and static must be declared after the access modifier.
Abstract class Studlycaps{abstract Public Function StudlyCaps (), final public static function Studlycapsone () {}}
if ($valueOne = = = $valueTwo) { //code ...} Switch ($valueThree) {case ' value ': //code ... break; Default: //code ... break;} do { //code ...} while ($valueFour <=), while ($valueFive <=) { //code ...} for ($i = 0; $i < $valueSix; $i + +) { //code ...} $demo = new Demo () $demo->dosomething ();d o_something ();
if ($valueOne = = = $valueTwo) { //code ...} Switch ($valueThree) {case ' value ': //code ... break; Default: //code ... break;} do { //code ...} while ($valueFour <=), while ($valueFive <=) { //code ...} for ($i = 0; $i < $valueSix; $i + +) { //code ...}
if ($valueOne = = = $valueTwo) {//Control structure (right Bien Hoa) left without space //code ...}
Coding recommendations
Heredoc syntax $sql = <<<sqlselect delivery_idfrom d_testwhere delivery_idin (123,234) GROUP by delivery_idhaving SUM (Send_number) <= 0; SQL;
if ($a > 0 && $b > 0 && $c > 0 && $d > 0 && $e > 0) {}
Public Function toolangfunction ( $valueOne = ', $valueTwo = ', $valueThree = ', $ Valuefour = ', $valueFive = ', $valueSix = ') { //coding ...}
$this->nametest->functionone () ->functiontwo () ->functionthree ();
$a = [ ' aaa ' = ' aaa ', ' bbb ' = ' BBB '];
Single quote multi-quote
No variables in string, single quotes
There are variables in the string, double quotes
$str = ' str '; $arg = "$str";
/** * Class Description * * desc */class standardexample{ /** * constant description. * * @var String */ const THIS_IS_A_CONST = '; /** * attribute description. * * @var String */public $nameTest = '; /** * constructor function. * * Constructor Description * @author name <email> * @param string $value parameter name/description * @return return value type return value Description * return value type: string,array,object,mixed (multiple, indeterminate), void (no return value) */ public function __construct ($value = ") { //coding ... }
/** * Member method name. * * Member Method Description * * @param string $value parameter name/description * * @example domain/api/controller/action?argu1=111&argu2=222 */public function testfunction ($value = ") { //code ...}
try { //coding ...} catch (\exception $e) { //coding ...}
Overwrite dosome to Dosomethingclass standardexample{ /** * Method List * * @var array */ private $_ FunctionList = []; Public function __construct ($functionList = Array ()) { $this->_functionlist = $value; } Public Function Dosome () { $this->functionone (); $this->functiontwo (); $this->functionthree (); $this->functionfour (); } Public Function dosomething () { foreach ($this->_functionlist as $function) { $this-$function ( ); } } ...}
// +----------------------------------------------------------------------// | Company Name XX Service//+----------------------------------------------------------------------//| Copyright (c) Http://domain All rights reserved.//+------------------------------------------------------------ ----------// | Author:name <email>//+----------------------------------------------------------------------
The above content to the novice programmer some of the code and suggestions for writing, hope to help everyone.