Best of 11 PHP programming specifications
From the beginning of the design, PHP was widely used to develop web-based applications. Since PHP is a scripting language, some specifications must be adhered to when developing.
This article will discuss commonly used good code practices, or code specifications, in the PHP field.
1, error Report open
Error reporting is a very useful feature in PHP and should be enabled in the development phase. This can help us determine the problem in our code. The most commonly used feature is "E_all", which helps us to discover all the warnings and critical errors. It must be pointed out that we put our code into the line before we should turn off this feature tip, otherwise it will leak all potential errors and warnings on the browser.
2, using the dry principle
' Do not Repeat yourself ', dry principle refers to not repeating your code. This concept is a very useful programming concept that can be hard to apply in any programming language, such as java,c# or PHP. Use the dry principle to ensure that we do not have redundant code in the program.
A solution that violates the DRY principle is often referred to as WET, referring to "write everything Twice", we write the same function code will appear more than a few times, do not say that you like typing.
Let's take a look at the following code:
LISTING1:
Wet Code mode:
1 |
$mysql = mysql_connect ( 'localhost' , 'mysqladmin_uid' , 'mysqladmin_pwd' ); |
2 |
mysql_select_db( 'DB_NAME' ) or die ( Sorry !! No database selected! ); |
The following uses the dry principle to optimize the code:
1 |
$db_host = ' localhost ' ; |
2 |
$db_user = ' mysqladmin_uid ' ; |
3 |
$db_password = ' mysqladmin_pwd ' ; |
4 |
$db_database = ' DB_NAME ' ; |
5 |
$mysql = mysql_connect( $db_host , $db_user , $db_password ); |
6 |
mysql_select_db( $db_database ); |
3, appropriate use of indentation and space
When writing code in any programming language, you must ensure that the Code provides the necessary indentation and sufficient space where needed. To increase the readability of your code and manage it in a more efficient way.
4, use meaningful and consistent naming standards
As with any programming language, PHP veterans also recommend that we follow meaningful naming standards. We have two main ways to implement this requirement:
A. Hump type
In this method, the first letter is lowercase, and the first letter of each word is then capitalized.
Listing2:
2 |
public void methodName(String argName) { |
B. Underline
In this method, we put an underscore ("_") between each of the two words. When using this method, the code can be modified as follows:
LISTING3: Using underlined code Snippets
2 |
public void method_name(String arg_name) { |
5, Avoid deep nesting
With any development language, multilevel nesting reduces the readability of the code. Any developer should avoid using deep nesting.
LISTING4: Code snippets have multiple levels of nesting
03 |
public function method_name( $arg_name ) { |
04 |
if ( is_writable ( $folder )) { |
05 |
if ( $fp = fopen ( $file_location_path , 'w' )) { |
06 |
if ( $stuff = extractSomeConditionalStuff ()) { |
07 |
if (fwrite ( $fp , $stuff )) { |
The code above is a simple nested code. We can see it is very difficult if the block ends where the figure is. For better readability, let's modify the code:
LISTING5: Code snippets to avoid multilevel nesting
01 |
function method_name (String arg_name) { |
03 |
if (! is_writable ( $folder )) { |
06 |
if (! $fp = fopen ( $file_location_path , 'w' )) { |
09 |
if (! $stuff = extractSomeConditionalStuff ()) { |
12 |
if (fwrite ( $fp , $stuff )) { |
6. Add the appropriate comments
In development, make sure you have enough in-line comments in your source code. This is a standard practice that should be adhered to. This helps in-depth analysis of the code, because normally the person responsible for encoding does not remain unchanged. Even if the same person is asked to make some simple changes in the code, the comments in the line will always help to understand what the code was written for. In order to use a good annotation standard in PHP, we recommend that you check out some standard PHP documentation packages, such as Phpdocumentor.
7, do not put the phpinfo () function in the site root directory
The Phpinfo () function is a very important feature and should be used with caution. With this feature, anyone can see more information about the server environment. It is best to always put
A secure location is used in the file and should be removed once the development is complete on-line.
8, never trust users
If your application involves any user input, it is important to write secure code to handle this approach, because it contains various possible inputs. To prevent the program from being attacked or destroying data integrity, be sure to verify the format of the filtered data. You can read this article, "Scary code injection."
9, reasonable use of caching mechanism
Good programming methods are always recommended to use caching mechanism caching to help us get better performance.
In the world of PHP, caching is used to implement:
Memcached-a store that stores key value pairs using small chunks of data.
apc--Optional PHP cache for open PHP opcode
xcache--a fast and reliable PHP opcode cache
Zend Cache–api, a collection that implements advanced caching capabilities.
eacclerator– Open Source Caching tool
10. Avoid copying existing variables
Copying a pre-defined variable to a local variable with a smaller name is not a good programming habit. This has an adverse effect on the performance of the application. Let's take a look at the following code snippet:
Listing6: Copying an existing variable
1 |
$desc = strip_tags ( $_POST [ 'PHP description' ]); |
The code above is an unnecessary example of copying a variable to a local variable. This is not a good practice. The same effect can be achieved by using the following code:
1 |
echo strip_tags ( $_POST [ 'PHP description' ]); |
11, using Frames
The framework was developed after a lot of research, so they proved to be less problematic. They make our development easier because they provide a proven solution. There are many frameworks available in PHP. You should take advantage of these during the development process. One of the frameworks that is widely adopted is the MVC or Model View controller.
Conclusion:
Programming Specifications guide us to develop code more efficiently.
Follow the programming specifications to ensure better performance of your application.
As in other programming languages, to create high-quality code, PHP also needs to follow this good programming specification.
http://www.bkjia.com/PHPjc/885675.html www.bkjia.com true http://www.bkjia.com/PHPjc/885675.html techarticle Best 11 PHP programming specifications from the beginning of the design, PHP was widely used to develop web-based applications. Since PHP is a scripting language, some specifications must be adhered to when developing. This article will ...