Best 11 PHP programming specifications-PHP Tutorial

Source: Internet
Author: User
The best 11 PHP programming specifications. The best 11 PHP programming specifications from the beginning of the design, PHP is widely used to develop Web-based applications. Because PHP is a scripting language, some rules must be followed during development. This article will provide the best 11 PHP programming specifications

PHP has been widely used to develop Web-based applications since its design. Because PHP is a scripting language, some rules must be followed during development.

This article will discuss common good code habits, or code specifications, in the PHP field.
1. enable error report

Error reporting is a very useful feature in PHP and should be enabled simultaneously in the development phase. This helps us determine the problems in our code. The most common feature is "E_ALL", which helps us identify all warnings and serious errors. It must be noted that we should disable this function before putting our code into service. otherwise, all potential errors and warnings will be exposed in the browser.

2. use the DRY principle

'Do not Repeat Yourself '. the DRY principle means not to Repeat your code .. This concept is a very useful programming concept that can be hard applied in any programming language, such as Java, C #, or PHP. Use the DRY principle to ensure that there is no redundant code in the program.
A solution that violates the DRY principle is usually called WET, which refers to "write everything twice". the code that we write with the same function will appear more than once.
Let's see the following code:
Listing1:
WET code method:

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. use indentation and spaces as appropriate.

When writing code in any programming language, make sure that the code is provided where necessary with appropriate indentation and spaces. To increase the readability of the code and manage the code in a more effective 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 achieve this:
A. camper
In this method, the first letter is lowercase, and the first letter of each word is followed by uppercase.
Listing2:

1 public class MyClass {
2 public void methodName(String argName) {
3 }
4 }

B. underline
In this method, we underline every two words ("_"). When using this method, the code can be modified as follows:
Listing3: use underline code snippets

1 public class MyClass {
2 public void method_name(String arg_name) {
3 }
4 }

5. avoid deep nesting

Multi-level nesting reduces code readability in any development language. Any developer should avoid using deep nesting.
Listing4: code snippets with multi-level nesting

01
02 class MyClass {
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 )) {
08 // ...
09 } else {
10 return false;
11 }
12 } else {
13 return false;
14 }
15 } else {
16 return false;
17 }
18 } else {
19 return false;
20 }
21 }
22 }
23 ?>

The above code is a simple nested code. We can see that it is very difficult if the block ends. For better readability, let's modify the code:
Listing5: code snippets to avoid multi-level nesting

01 function method_name (String arg_name) {
02 // ...
03 if (! is_writable ( $folder )) {
04 return false;
05 }
06 if (! $fp = fopen ( $file_location_path, 'w' )) {
07 return false;
08 }
09 if (! $stuff = extractSomeConditionalStuff ()) {
10 return false;
11 }
12 if (fwrite ( $fp, $stuff )) {
13 // ...
14 } else {
15 return false;
16 }
17 }

6. add the expected comments.

During development, make sure that your source code has enough line comments. This is a standard practice that should be followed. This helps to analyze the code in depth, because the person in charge of the encoding will not always remain unchanged. Even if the same person is asked to make some simple changes in the code, comments in the line will always help to understand what code is used for at the time. To use better annotation standards in PHP, we recommend that you check some standard PHP document packages, such as phpjavasentor.

7. do not put the PHPINFO () function in the root directory of the website.

The phpinfo () function is a very important function and should be used with caution. With this function, anyone can view the details of the server environment. It is best to always put
Use this code in a secure file.

8. never trust users

If your application involves any user input, you must write secure code to handle it in this way because it contains various possible inputs. To avoid program injection attacks or damage data integrity, you must verify the data filtering format. You can read this article "terrible code injection methods"

9. make rational use of the cache mechanism

Good programming methods are always recommended to use the cache mechanism cache to help us achieve better performance.

In the PHP world, cache is used for implementation:
Memcached-a key-value pair stored in small data block storage.
APC -- Optional PHP Cache for open PHP operation code cache
XCache-a fast and reliable PHP operation code cache
Zend Cache-API is a set of advanced high-speed Cache functions.
EAcclerator-open-source cache tool

10. avoid copying existing variables.

Copying a predefined variable to a local variable with a small name is not a good programming habit. This adversely affects the performance of applications. Let's take a look at the following code snippet:
Listing6: copy existing variables

1 $desc = strip_tags($_POST['PHP description']);
2 echo $desc;

The code above is an example of copying a variable as a local variable that is not necessary. Is this a good practice. To achieve the same effect, you can use the following code:

1 echo strip_tags($_POST['PHP description']);

11. use the framework

Frameworks are developed after a large amount of research, so they prove to be less problematic. They make development easier because they provide mature solutions. There are many available frameworks in PHP. You should use this in the development process. One of the frameworks is widely used in MVC or model view controllers.

Conclusion:

Programming specifications guide us to develop code more effectively.
Follow programming specifications to ensure better application performance.
As in other programming languages, to create high-quality code, PHP also needs to follow this good programming specification.

Since its design, ghost PHP has been widely used to develop Web-based applications. Because PHP is a scripting language, some rules must be followed during development. This article will...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.