The 11 Best PHP Programming Guidelines
From its inception, PHP has been widely used to develop web-based applications. Since PHP is a scripting language, some specifications must be followed during development.
This article will discuss commonly used good coding habits, or coding standards, in the field of PHP.
1. Error reporting is enabled
Error reporting is a very useful feature in PHP and should be enabled during the development phase. This can help us identify problems in our code. The most commonly used function is "E_ALL", which helps us find all warnings and critical errors. It must be pointed out that before we put our code online, we should turn off this feature prompt, otherwise all potential errors and warnings will be exposed in the browser.
2. Use the DRY principle
‘Do not Repeat Yourself’, the DRY principle refers to do not repeat your code. This concept is a very useful programming concept that can be applied in any programming language such as Java, C# or PHP. Use the DRY principle to ensure that we have no redundant code in our programs.
Solutions that violate the DRY principle are often called WET, which refers to "write everything twice". The code we write with the same function will appear more than once, and everyone jokingly calls it like typing.
Let’s take a look at 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 indents and spaces appropriately
When writing code in any programming language, you must ensure that the code provides proper indentation and sufficient spaces where necessary. To increase the readability of the code and manage the code 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 achieve this requirement:
A. Camel case
In this method, the first letter is lowercase, and the first letter of each word is followed by uppercase.
Listing2:
2 |
public void methodName(String argName) {
|
B. Underline
In this method, we put an underscore ("_") between every two words. When using this method, the code can be modified as follows:
Listing3: Use underlined code snippets
2 |
public void method_name(String arg_name) {
|
5, avoid deep nesting
Using any development language, multiple levels of nesting reduce the readability of the code. Any developer should avoid using deep nesting.
Listing4: The code snippet has 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 above code is a simple nested code. We can see that 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 multi-level 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 appropriate comments
During development, please make sure you have enough inline comments in your source code. This is a standard practice that should be followed. This helps with in-depth analysis of the code because often the person responsible for coding doesn't stay the same. Even if the same person is asked to make some simple changes in the code, inline comments will always help understand what the code was written for at the time. In order to use better annotation standards in PHP, it is recommended that you check out some standard PHP documentation packages, such as phpDocumentor.
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. Using this feature, anyone can see the details of the server environment. It's best to always put it in
Used in a file in a safe location. Once development is completed and online, this code should be deleted.
8. Never trust users
If your application involves any user input, it is important to write safe code to handle it this way, as it contains a variety of possible inputs. To prevent programs from being injected by attacks or damaging data integrity, you must verify the format of the filtered data. You can read this article "The Horrible Code Injection Method"
9. Reasonable use of caching mechanism
Good programming method always recommends using caching mechanism. Caching helps us get better performance.
In the PHP world, caching is implemented using:
Memcached - A key-value store that uses small chunks of data to store.
APC - Optional PHP cache for open PHP opcode cache
XCache - A fast and reliable PHP opcode cache
Zend Cache – API for implementing a collection of advanced caching features.
eAcclerator – open source caching tool
10, avoid copying existing variables
Copying predefined variables to local variables with smaller names is not a good programming practice. This adversely affects the performance of the application. Let’s take a look at the following code snippet:
Listing6: Copy existing variables
1 |
$desc = strip_tags ( $_POST [ 'PHP description' ]);
|
The code above is an example of unnecessary copying of a variable as 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
Frameworks are developed after extensive research and therefore, they prove to be less problematic. They make our development easier because they provide proven solutions. There are many frameworks available in PHP. You should take advantage of these during development. One of the frameworks that is widely adopted is MVC or Model View Controller.
Conclusion:
Programming standards guide us to develop code more efficiently.
Follow programming practices to ensure better performance of your application.
Just like in other programming languages, PHP also needs to follow this good programming practice in order to create high-quality code.
http://www.bkjia.com/PHPjc/885675.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/885675.htmlTechArticleBest 11 PHP Programming Specifications From the beginning of its design, PHP has been widely used to develop web-based applications. Since PHP is a scripting language, some specifications must be followed during development. This article will...