PHP programming style specification sharing_php basics
Note: This specification is compiled by EasyChen based on the "C Development Specification" of the SINA Network Application Development Department, the "PHP4 Development Specification" of the Interactive Technology Department, and the phpDocument specification. I think it is very good and suitable for PHP development. It is a reference for everyone. It is very necessary to develop a good programming style.
Chapter 1 Naming Convention
1.1 Variables
1.1.1 Global variables
Global variables start with $g_, such as $g_data_list.
1.1.2 General variables
Generally, variables are named with lowercase letters, and words are separated by underscores.
Variable names should be in the form of nouns or adjectives. Such as $value, $new_value.
1.1.3 Temporary variables
Do not use temporary variables such as $i, $j, etc. that are frequently used in loops for other purposes.
1.2 Function
Functions are named with lowercase letters, and words are separated by underscores.
It is recommended to use verb noun when naming functions, such as get_user_img.
The functions that complete a set of functions are placed in a file, and the file storing the functions is named function_name.func.php.
1.3 Class
The class uses English uppercase and lowercase to separate words, including the first word, and the first letter of all words is capitalized, such as PageManager;
In a class, put methods before attribute definitions and public methods before special methods;
Generally, a class corresponds to a file;
When some classes are closely related, they can be stored in one file;
The file that stores the class is named ClassName.class.php.
1.4 Method
The method uses English uppercase and lowercase to separate words. Except for the first word, the first letters of other words are capitalized, such as getCurrentPage();
Do not use uncommon abbreviations, such as where2go();
When using commonly used abbreviations, only capitalize the first letter, such as getHtml().
Chapter 2 Format Rules
2.1 Semantic Separation
Blank lines should be used between each function and method;
There is no need for line breaks between closely related statements in the same function. In other cases, line breaks are required.
2.2 Space rules
2.2.1 Spaces must be added before and after logical operators
Correct
Error
$a==$b;
$ a ==$b;
Correct
Error
Remarks: The addition and subtraction operators cannot add spaces.
2.2.2 Spaces must be added when separating multiple parameters
Correct
$g_pro , $g_user , g_show;
get_db_info($host, $user, $passwd);
Error
$g_pro,$g_user,$g_show;
get_db_info($host,$user,$passwd);
2.2.3 Spaces must be added after syntax keywords
For example: If, for, while, switch…..
Correct
Error
2.3 String and variable connection rules
When strings and variables are connected using the '.' sign, spaces must be added before and after the '.'. When using the "." sign, "{}" must be added before and after the variable.Correct
$my_name = 'file_' . $var1; $my_name = "file_{$var1}";
Error
$my_name = "file_'.$var1; $my_name = "file_$var1";
2.4 Parentheses Rules
There is no need to add spaces after the function name, and spaces must be added after the syntax keywords.
Correct
Error
2.5 Curly Brace Rules
The curly braces must correspond to the upper and lower parts.
Correct
if ($a)
{
$b = $a;
}
Error
if ($a){
$b = $a;
}
2.6 Array definition rules
When defining and using an array, single quotes must be added before and after the key value.
PHP code:
Correct
array( 'name' => '', ' gender' => '' );
$user_info['name'];
Error
array( name => '', gender => '' );
$user_info[name];
2.7 SQL Rules
SQL statement keywords embedded in PHP should all be in uppercase;
Table names and field names should be enclosed in backticks (`) to prevent errors caused by spaces in the field names. An error occurred;
The data value should be surrounded by single quotes'', and you should ensure that the single quotes in the data value have been escaped to prevent SQL injection.
Correct
Error
Chapter 3 Comment Rules
3.1 General Rules
Do not write unnecessary comments; only when the code cannot explain the logic well, use comments to supplement it;
Think of comments as Part of the program, write/maintain comments while writing/maintaining code;
The comments completely adopt the specifications of PHPDocumentor to facilitate the generation of API-level documents.
3.2 Detailed rules
Please refer to the PHPDocumentor manual. Annotation examples for each part are given below.
3.2.1 Copyright information
Annotation name Copyright information
Example of annotation:
//
// ---- ----------------------------------------
// | phpDocumentor |
// --------------------------------------- -------------
// | Copyright (c) 2000-2003 Joshua Eichorn |
// | Email jeichorn@phpdoc.org |
// | Web http ://www.phpdoc.org |
// ---------------------------------------- ----------------
// | This source file is subject to PHP License |
// -------------- -------------------------------------
//
Note: Use // to mark copyright information to avoid conflict with PHPDocumentor's page-level DocBlock
3.2.2 File header comment example
Comment name file header comment
Comment Demonstration:
PHP code:
/**
* All abstract representations of inline tags are in this file
* @package phpDocumentor
* @subpackage InlineTags
* @since separate file since version 1.2
* @version $Id $
*/
div>
Remarks
1) The file header comment needs to indicate the package and sub-package it belongs to;
2) Add $ID to @version to facilitate file management using CVS.
3.2.3 Class annotation example
Annotation name Class annotation
Example of annotation:
PHP code:
/**
* Use this element to represent an {@}inline tag} like {@}link}
* @see parserStringWithInlineTags
* @package phpDocumentor
* @subpackage InlineTags
* @author Greg Beaver
* @since 1.0rc1
* @version $Revision: 1.21.2.6 $
* @tutorial inlinetags.pkg
*/
3.2. 4 Class Attribute Annotation Example
Annotation Name Class Attribute Annotation
Annotation Demonstration:
PHP Code:
/**
* Element type
*
* Type is used by many functions to skip the hassle of
*
*
<br>* if get_class($blah) == 'parserBlah'<br>*
* always "inlinetag"
* @var string
*/
var $ type = 'inlinetag';
3.2.5 Example of function/class method annotation
Annotation name Function/class method annotation
Example of annotation:
PHP code:
/**
* @return string always ''
* calculate the short description of a DocBlock
* @see parserStringWithInlineTags::getString()
* @see parserStringWithInlineTags::trimmedStrlen()
*/
function getString(){
return '';
}

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Comply with PHP writing specifications: Improve teamwork and code collaborative development capabilities Introduction: In software development, code quality and teamwork are crucial. Complying with programming standards is one of the effective means to improve code quality and teamwork. This article will focus on how to comply with PHP writing standards to improve teamwork and code collaborative development capabilities. 1. Naming conventions Good naming conventions can increase the readability and maintainability of code. In PHP programming, we recommend following the following naming convention: Use camelCase naming for variables and functions, such as

Programming disciplines are crucial to ensure code quality and maintainability, especially when developing PHP applications. One of the common requirements is efficient validation of input strings to ensure that they contain only numeric and alphabetic characters. This article will introduce how to write code in PHP to achieve this requirement while following programming conventions. Overview of Programming Standards In PHP programming, following certain programming standards can make the code easier to read and maintain, while helping to reduce errors and improve code performance. Here are some programming guideline recommendations: Use intentional

Explore the secrets of PHP writing specifications: In-depth understanding of best practices Introduction: PHP is a programming language widely used in web development. Its flexibility and convenience allow developers to use it widely in projects. However, due to the characteristics of the PHP language and the diversity of programming styles, the readability and maintainability of the code are inconsistent. In order to solve this problem, it is crucial to develop PHP writing standards. This article will delve into the mysteries of PHP writing disciplines and provide some best practice code examples. 1. Naming conventions compiled in PHP

Detailed explanation of PHP writing specifications: Create amazing coding style Introduction: In the field of software development, excellent coding style is a programmer's advantage. PHP is a commonly used programming language. Good writing standards can improve the readability, maintainability and collaboration of the code. This article will introduce PHP writing specifications in detail to help you create an amazing coding style. 1. Naming specifications 1.1 Naming variables and functions Variables and functions should use meaningful and clear names, using a combination of lowercase letters and underscores. Variable names should use camelCase

Best practices for PHP writing specifications: Write clean and elegant code Introduction: In PHP development, writing clean and elegant code is the key to improving code quality and maintainability. This article will explore several best practices to help developers write high-quality PHP code, thereby improving the maintainability and readability of the project. 1. Unified coding standards In a project, the coding styles of different developers may vary greatly, which is a huge challenge to the readability and maintainability of the code. Therefore, it is very important to develop and adhere to unified coding standards.

Best practices for writing specifications in PHP: Creating an efficient and maintainable code base Introduction: With the rapid development of Internet technology, PHP has become one of the most popular development languages. As a flexible scripting language, PHP has unparalleled advantages in building dynamic websites and web applications. However, if we don’t follow some PHP coding best practices, our codebase can become unmaintainable, unstable, and inefficient. This article will introduce some noteworthy PHP coding standards to help developers create efficient

Implementation strategies for PHP writing specifications: ensuring high efficiency of team development In today's software development field, team collaboration has become the norm. In order to ensure high efficiency of team development, writing specifications has become an essential link. This article will introduce the implementation strategy of PHP writing specifications, with code examples to help the development team better understand and apply these specifications. Using consistent naming conventions is one of the important factors in code readability and maintainability. Team members should agree on consistent naming rules to ensure code consistency and readability

Practice PHP writing standards: Tips for improving code structure and layout Introduction: In PHP development, good code structure and layout are very important, it can help us improve code readability, maintainability and scalability. This article will introduce some practical techniques to improve the structure and layout of PHP code, as well as corresponding code examples. 1. Use appropriate file and directory structures. A good file and directory structure can help us better organize and manage our code. Generally, we can organize files and directories in the following way: put related classes
