Home >Backend Development >PHP Tutorial >PHP coding standards and suggestions for newbies
If you want to be an excellent programmer, good coding standards are very important. So how can you write good code? In this article, we will give you some PHP coding standards and suggestions.
PHP code files must start with the
<?php //开头 // 不结尾
PHP code files must be encoded in UTF-8 without BOM.
例sublime, setting增加,"show_encoding" : true
The number of characters per line should not exceed 80 characters
例,sublime "word_wrap": "true", "wrap_width": 80,
Tap key 4 spaces
例,sublime "tab_size": 4,
Only classes (traits) should be defined in PHP code /Function/Constant/Other operations that will produce side effects (such as generating file output and modifying .ini configuration files, etc.), you can only choose one.
例, a.php class A { } b.php function demo() { } c.php define('A', value); d.php ini_set('some_vars', value);
The naming of classes/trait/Interface must follow the StudlyCaps camel case naming convention starting with an uppercase letter.
class StudlyCaps { } trait StudlyCaps { } Interface StudlyCaps { }
Constants in a class must have all letters in capital letters, and words separated by underscores.
define('FOO_BAR', 'something more'); const FOO_BAR = value;
The method name (in class/trait) must conform to the camelCase-style camel case naming convention starting with lowercase.
class StudlyCaps { public function studlyCaps() { // coding... } }
Function names must conform to the snake_case-style underscore naming convention.
function snake_case() { // coding... }
Private (private) method (in class/trait) name must conform to _camelCase-style leading underscore lowercase beginning camel case Naming conventions.
class StudlyCaps { private function _studlyCaps() { // coding... } }
The first word of the method name is a verb.
class StudlyCaps { public function doSomething() { // coding... } }
Variables must conform to the camelCase-style naming convention of camel case starting with lowercase.
class StudlyCaps { public function doSomething() { $someVariable = 'demo'; // coding... } }
When a method/function has multiple parameters, there must be a space between them
class StudlyCaps { public function doSomething($variableOne, $variableTwo) { // coding... } }
$a = $b + $c; $a = $b . $c;
namespace Standard; // 空一行 use Test\TestClass;//use引入类 // 空一行
class StudlyCaps { }
class StudlyCaps { public function studlyCaps() { // coding... } } function snake_case() { // coding... }
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 <= 10); while ($valueFive <= 10) { // code... } for ($i = 0; $i < $valueSix; $i++) { // code... } $demo = new Demo() $demo->doSomething(); do_something();</p></li></ul> <p></p> <ul class=" list-paddingleft-2"><li>The opening brace { of the control structure must be written on the same line as the declaration, and the closing brace } must be written after the body In a line of its own. <p></p> </li></ul> <pre class="brush:php;toolbar:false">if ($valueOne === $valueTwo) { // code... } switch ($valueThree) { case 'value': // code... break; default: // code... break; } do { // code... } while ($valueFour <= 10); while ($valueFive <= 10) { // code... } for ($i = 0; $i < $valueSix; $i++) { // code... }
if ($valueOne === $valueTwo) {// 控制结构(右边和)左边不加空格 // code... }Encoding suggestions
// heredoc语法 $sql = <<<SQL SELECT delivery_id FROM d_test WHERE delivery_id IN (123,234) GROUP BY delivery_id HAVING 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' ];
$str = 'str'; $arg = "$str";
/** * 类描述 * * desc */ class StandardExample { /** * 常量描述. * * @var string */ const THIS_IS_A_CONST = ''; /** * 属性描述. * * @var string */ public $nameTest = ''; /** * 构造函数. * * 构造函数描述 * @author name <email> * @param string $value 形参名称/描述 * @return 返回值类型 返回值描述 * 返回值类型:string,array,object,mixed(多种,不确定的),void(无返回值) */ public function __construct($value = '') { // coding... }
/** * 成员方法名称. * * 成员方法描述 * * @param string $value 形参名称/描述 * * @example domain/api/controller/action?argu1=111&argu2=222 */ public function testFunction($value = '') { // code... }
try { // coding... } catch (\Exception $e) { // coding... }
// 改写doSome为doSomething class StandardExample { /** * 方法列表 * * @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服务 // +---------------------------------------------------------------------- // | Copyright (c) 2017 http://domain All rights reserved. // +---------------------------------------------------------------------- // | Author: name <email> // +----------------------------------------------------------------------
以上内容就给新手程序员的一些编写代码的规范及建议,希望能帮助到大家。
相关推荐:
The above is the detailed content of PHP coding standards and suggestions for newbies. For more information, please follow other related articles on the PHP Chinese website!