Top 10 tips for PHP developers to get twice the result with half the effort

Source: Internet
Author: User
Tags php template php website how to use sql
What will happen if you use a large mirror as a surf board? Maybe you will conquer the waves in a short period of time, but you must understand from the bottom of your heart that this is not the right choice for surfing. The same principle applies to PHP programming, although such an analogy sounds odd. What happens if you use a large mirror as a surfboard? Maybe you will conquer the waves in a short period of time, but you must understand from the bottom of your heart that this is not the right choice for surfing. The same principle applies to PHP programming, although such an analogy sounds odd. We often hear someone trying to learn PHP over a weekend, but with all my respect, this is a very bad way to learn this programming language.

Why is the process of learning PHP different from any other language?

In essence, if you know how to "do things" in PHP, you will be comfortable using it. Therefore, it is worth your effort to understand these methods. In PHP, simply solving the problem based on your own ideas is often a wrong approach. This is not because you are a bad programmer, but because if you want to write good maintainability code, you must use some standard skills. Let's take a look at the 10 tips you need to know.

1. how to create a website Index page correctly

When creating a website, creating a website index page is one of the first tasks. If you are a beginner in PHP, the typical practice when writing an index page is to program only the content required for the index page, and create another page through other links. However, if you want to learn a more efficient way to implement PHP programming, you can use "index. php? Page = home "mode, which is used by many websites.

2. use Request Global Array to capture data

In fact, we have no reason to use the $ _ GET and $ _ POST arrays to capture values. $ _ REQUEST: The Global array allows you to obtain a get or form REQUEST. Therefore, in most cases, the more efficient code for parsing data is roughly as follows:

 
 
  1. $ Action = isset (what happens if you use a large mirror as a surfboard? Maybe you will conquer the waves in a short period of time, but you must understand from the bottom of your heart that this is not the right choice for surfing. The same principle applies to PHP programming, although such an analogy sounds odd. We often hear someone trying to learn PHP over a weekend, but with all my respect, this is a very bad way to learn this programming language.

    Why is the process of learning PHP different from any other language?

    In essence, if you know how to "do things" in PHP, you will be comfortable using it. Therefore, it is worth your effort to understand these methods. In PHP, simply solving the problem based on your own ideas is often a wrong approach. This is not because you are a bad programmer, but because if you want to write good maintainability code, you must use some standard skills. Let's take a look at the 10 tips you need to know.

    1. how to create a website Index page correctly

    When creating a website, creating a website index page is one of the first tasks. If you are a beginner in PHP, the typical practice when writing an index page is to program only the content required for the index page, and create another page through other links. However, if you want to learn a more efficient way to implement PHP programming, you can use "index. php? Page = home "mode, which is used by many websites.

    2. use Request Global Array to capture data

    In fact, we have no reason to use the $ _ GET and $ _ POST arrays to capture values. $ _ REQUEST: The Global array allows you to obtain a get or form REQUEST. Therefore, in most cases, the more efficient code for parsing data is roughly as follows:

    ___FCKpd___0

    3. use var_dump to Debug PHP code

    If you are looking for php debugging technology, I must say that var_dump should be your target. This command can meet all your needs in displaying php information. Most of the debugging code is related to obtaining the value in PHP.

    4. PHP processes the code logic and Smarty processes the presentation layer.

    Smarty is a PHP template engine written using PHP. it is one of the most famous PHP template engines in the industry. It separates the logic code from the external content and provides an easy-to-manage and use method to combine the originally HTML code with the PHP code logic separation. Simply put, the purpose is to separate PHP programmers from front-end personnel so that programmers can change the logic content of the program without affecting the front-end personnel's page design, the re-modification of the page by the front-end personnel does not affect the program logic, which is particularly important in projects with multiple partners.

    5. create a Config file when global values are required.

    It is a bad practice to easily create a global value, but sometimes the actual situation does need to be done. It is a good idea to use global values for database tables or database connection information, but do not use global values frequently in your PHP code. In addition, a better way is to store your global variables in a config. php file.

    6. if not defined, access is prohibited!

    If you have created a page correctly, no one else can access the index. php page other than index. php or home. php. Once index. php is accessed, you can open the required page by obtaining the variable. Your index page should contain the following code:

        
        
    1. define('yourPage',1);

    Then, other pages should include:

        
        
    1. if (!defined('yourPage')) die('Access Denied');

    This is intended to prevent direct access to your other php pages. In this way, anyone who tries to access other web pages without using index. php will receive a "access denied" message.

    7. create a database class

    If you are programming a database (a very common task in PHP), a good idea is to create a database class to handle any database management function. The sample code is as follows:

        
        
    1. public function dbExec($query)
    2. {
    3. $result = $this->db->exec($query);
    4. if (PEAR::isError($result))
    5. errorRedirect($result->getMessage(), true);
    6. else
    7. return $result;
    8. }

    This function receives and executes only one query statement. It also handles any possible errors. You can also include the audit code here, but I prefer to use a similar audit function:

        
        
    1. // checks if arguments given are integer values not less than 0 - has multiple arguments
    2. function sanitizeInput()
    3. {
    4. $numargs = func_num_args();
    5. $arg_list = func_get_args();
    6. for ($i = 0; $i < $numargs; $i++) {
    7. if (!is_numeric($arg_list[$i]) || $arg_list[$i] < 0)
    8. errorRedirect("Unexpected variable value", true);
    9. }
    10. }

    8. a php file is used to process input and a class. php file is used to process specific functions.

    An important way to avoid code confusion is to redirect user input to other functions for processing. The principle is very simple. the php file obtains any input we need and redirects its execution to a function in the class file. For example, suppose there is a file similar to index. php? Page = profile & action = display "URL. The URL is retrieved by profile. php and the operation is "display ". Then we use a simple switch function to execute the real Display function:

        
        
    1. require_once PROJECTROOT.'libs/messages.class.php';
    2. $message = new Message();
    3. switch ($action)
    4. {
    5. case 'display':
    6. $message->display();
    7. break;
    8. ...

    As shown above, I used a message class and started the switch check. $ Message is only an object used by calling functions in the class.

    9. understand your SQL statements and always review them (Sanitize)

    As I mentioned earlier, 99% of the most important parts of any php website may be databases. Therefore, you need to be familiar with how to use SQL correctly. Learn to join tables and more advanced technologies. The following shows a function example using MySQL and uses the 7th functions described in this article for review.

        
        
    1. private function getSentMessages($id)
    2. {
    3. $this->util->sanitizeInput($id);
    4. $pm_table = $GLOBALS['config']['privateMsg'];
    5. $users = $GLOBALS['config']['users'];
    6. $sql = "SELECT PM.*, USR.username as name_sender FROM $pm_table PM, $users USR
    7. WHERE id_sender = '$id' AND sender_purge = FALSE AND USR.id = PM.id_receiver AND is_read = TRUE
    8. ORDER BY date_sent DESC";
    9. $result = $this->dbQueryAll($sql);
    10. return $result;
    11. }

    First, we check the user input (pass the message id through a GET variable), and then we execute our SQL command. Note the SQL usage here. You need to know how to use aliases and join tables.

    10. when you only need one object, use the Singleton mode.

    In a common case in PHP, we only need to create an object once and use it in our entire program. A good example is the smarty variable. once initialized, it can be used anywhere. A good solution to this situation is the singleton mode. The sample code is as follows:

        
        
    1. function smartyObject()
    2. {
    3. if ($GLOBALS['config']['SmartyObj'] == 0)
    4. {
    5. $smarty = new SmartyGame();
    6. $GLOBALS['config']['SmartyObj'] = $smarty;
    7. }
    8. else
    9. $smarty = $GLOBALS['config']['SmartyObj'];
    10. return $smarty;
    11. }

    Note that we have a global smarty variable (in this example it is initialized in config. php). if its value is 0, we will create a new smarty object. Otherwise, it means that the object has been created, and we only need to return it.

    REQUEST ['action'])? What will happen if you use a large mirror as a surf board? Maybe you will conquer the waves in a short period of time, but you must understand from the bottom of your heart that this is not the right choice for surfing. The same principle applies to PHP programming, although such an analogy sounds odd. We often hear someone trying to learn PHP over a weekend, but with all my respect, this is a very bad way to learn this programming language.

    Why is the process of learning PHP different from any other language?

    In essence, if you know how to "do things" in PHP, you will be comfortable using it. Therefore, it is worth your effort to understand these methods. In PHP, simply solving the problem based on your own ideas is often a wrong approach. This is not because you are a bad programmer, but because if you want to write good maintainability code, you must use some standard skills. Let's take a look at the 10 tips you need to know.

    1. how to create a website Index page correctly

    When creating a website, creating a website index page is one of the first tasks. If you are a beginner in PHP, the typical practice when writing an index page is to program only the content required for the index page, and create another page through other links. However, if you want to learn a more efficient way to implement PHP programming, you can use "index. php? Page = home "mode, which is used by many websites.

    2. use Request Global Array to capture data

    In fact, we have no reason to use the $ _ GET and $ _ POST arrays to capture values. $ _ REQUEST: The Global array allows you to obtain a get or form REQUEST. Therefore, in most cases, the more efficient code for parsing data is roughly as follows:

    ___FCKpd___0

    3. use var_dump to Debug PHP code

    If you are looking for php debugging technology, I must say that var_dump should be your target. This command can meet all your needs in displaying php information. Most of the debugging code is related to obtaining the value in PHP.

    4. PHP processes the code logic and Smarty processes the presentation layer.

    Smarty is a PHP template engine written using PHP. it is one of the most famous PHP template engines in the industry. It separates the logic code from the external content and provides an easy-to-manage and use method to combine the originally HTML code with the PHP code logic separation. Simply put, the purpose is to separate PHP programmers from front-end personnel so that programmers can change the logic content of the program without affecting the front-end personnel's page design, the re-modification of the page by the front-end personnel does not affect the program logic, which is particularly important in projects with multiple partners.

    5. create a Config file when global values are required.

    It is a bad practice to easily create a global value, but sometimes the actual situation does need to be done. It is a good idea to use global values for database tables or database connection information, but do not use global values frequently in your PHP code. In addition, a better way is to store your global variables in a config. php file.

    6. if not defined, access is prohibited!

    If you have created a page correctly, no one else can access the index. php page other than index. php or home. php. Once index. php is accessed, you can open the required page by obtaining the variable. Your index page should contain the following code:

    ___FCKpd___1

    Then, other pages should include:

    ___FCKpd___2

    This is intended to prevent direct access to your other php pages. In this way, anyone who tries to access other web pages without using index. php will receive a "access denied" message.

    7. create a database class

    If you are programming a database (a very common task in PHP), a good idea is to create a database class to handle any database management function. The sample code is as follows:

    ___FCKpd___3

    This function receives and executes only one query statement. It also handles any possible errors. You can also include the audit code here, but I prefer to use a similar audit function:

    ___FCKpd___4

    8. a php file is used to process input and a class. php file is used to process specific functions.

    An important way to avoid code confusion is to redirect user input to other functions for processing. The principle is very simple. the php file obtains any input we need and redirects its execution to a function in the class file. For example, suppose there is a file similar to index. php? Page = profile & action = display "URL. The URL is retrieved by profile. php and the operation is "display ". Then we use a simple switch function to execute the real Display function:

    ___FCKpd___5

    As shown above, I used a message class and started the switch check. $ Message is only an object used by calling functions in the class.

    9. understand your SQL statements and always review them (Sanitize)

    As I mentioned earlier, 99% of the most important parts of any php website may be databases. Therefore, you need to be familiar with how to use SQL correctly. Learn to join tables and more advanced technologies. The following shows a function example using MySQL and uses the 7th functions described in this article for review.

    ___FCKpd___6

    First, we check the user input (pass the message id through a GET variable), and then we execute our SQL command. Note the SQL usage here. You need to know how to use aliases and join tables.

    10. when you only need one object, use the Singleton mode.

    In a common case in PHP, we only need to create an object once and use it in our entire program. A good example is the smarty variable. once initialized, it can be used anywhere. A good solution to this situation is the singleton mode. The sample code is as follows:

    ___FCKpd___7

    Note that we have a global smarty variable (in this example it is initialized in config. php). if its value is 0, we will create a new smarty object. Otherwise, it means that the object has been created, and we only need to return it.

    REQUEST ['action']: 0;

3. use var_dump to Debug PHP code

If you are looking for php debugging technology, I must say that var_dump should be your target. This command can meet all your needs in displaying php information. Most of the debugging code is related to obtaining the value in PHP.

4. PHP processes the code logic and Smarty processes the presentation layer.

Smarty is a PHP template engine written using PHP. it is one of the most famous PHP template engines in the industry. It separates the logic code from the external content and provides an easy-to-manage and use method to combine the originally HTML code with the PHP code logic separation. Simply put, the purpose is to separate PHP programmers from front-end personnel so that programmers can change the logic content of the program without affecting the front-end personnel's page design, the re-modification of the page by the front-end personnel does not affect the program logic, which is particularly important in projects with multiple partners.

5. create a Config file when global values are required.

It is a bad practice to easily create a global value, but sometimes the actual situation does need to be done. It is a good idea to use global values for database tables or database connection information, but do not use global values frequently in your PHP code. In addition, a better way is to store your global variables in a config. php file.

6. if not defined, access is prohibited!

If you have created a page correctly, no one else can access the index. php page other than index. php or home. php. Once index. php is accessed, you can open the required page by obtaining the variable. Your index page should contain the following code:

___FCKpd___1

Then, other pages should include:

___FCKpd___2

This is intended to prevent direct access to your other php pages. In this way, anyone who tries to access other web pages without using index. php will receive a "access denied" message.

7. create a database class

If you are programming a database (a very common task in PHP), a good idea is to create a database class to handle any database management function. The sample code is as follows:

___FCKpd___3

This function receives and executes only one query statement. It also handles any possible errors. You can also include the audit code here, but I prefer to use a similar audit function:

___FCKpd___4

8. a php file is used to process input and a class. php file is used to process specific functions.

An important way to avoid code confusion is to redirect user input to other functions for processing. The principle is very simple. the php file obtains any input we need and redirects its execution to a function in the class file. For example, suppose there is a file similar to index. php? Page = profile & action = display "URL. The URL is retrieved by profile. php and the operation is "display ". Then we use a simple switch function to execute the real Display function:

___FCKpd___5

As shown above, I used a message class and started the switch check. $ Message is only an object used by calling functions in the class.

9. understand your SQL statements and always review them (Sanitize)

As I mentioned earlier, 99% of the most important parts of any php website may be databases. Therefore, you need to be familiar with how to use SQL correctly. Learn to join tables and more advanced technologies. The following shows a function example using MySQL and uses the 7th functions described in this article for review.

___FCKpd___6

First, we check the user input (pass the message id through a GET variable), and then we execute our SQL command. Note the SQL usage here. You need to know how to use aliases and join tables.

10. when you only need one object, use the Singleton mode.

In a common case in PHP, we only need to create an object once and use it in our entire program. A good example is the smarty variable. once initialized, it can be used anywhere. A good solution to this situation is the singleton mode. The sample code is as follows:

___FCKpd___7

Note that we have a global smarty variable (in this example it is initialized in config. php). if its value is 0, we will create a new smarty object. Otherwise, it means that the object has been created, and we only need to return it.

Related Article

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.