PHP coding standards

不言
Release: 2023-03-22 19:52:02
Original
1497 people have browsed it

This article shares with you the coding rules for PHP. It is very practical. Friends in need can use it as a reference.

File name:

Category:
DemoTest.class.php
Interface:
DemoTest.interface.php
Others:
demoTest.{style}.php


Naming convention:

整体采用驼峰命名法,同时命名要有意义。
Copy after login

Global variables:
Use "_" on both sides, and use camel case naming in the middle

Constant:
                                                                                                                                                                                                                                                                             # Camel case naming method can be preceded by an abbreviation indicating the variable type. Accurate and meaningful English words must be used:

​ ​ ​ String: $sMyName

​ ​ ​ Array: $arrMyArray

Interface and class naming:
CamelCase nomenclature, starting with a capital letter.

​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​ together with the class file name:

For example: Class DataBase, file name: DataBase.class.php


Comments:

程序应提供必要的注释,包括文件注释,代码块注释,函数注释。
Copy after login

File comments:

At the head of the entire file:

​ ​ ​ ​ /**

            *
           */

Function comments:
//Place before function

Code block comment:
//Only write code comments where necessary to explain why, not what to do, and provide the most useful information.


Code style:


Indentation and spaces:

         Two spaces indent

Spaces flow out before and after the assignment character


Statement line breaks:
​ ​ Control the line length within 80 characters


Better practice:


Use PHP's existing variables instead of defining them yourself.

For line breaks, use PHP_EOL instead of \n or \r\n

Related recommendations:

How to solve php encoding conversion garbled code

Detailed explanation of PHP encoding conversion function application skills_php examples

PHP Coding Style Guide (PHP-FIG PSR-2)

The above is the detailed content of PHP coding standards. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!