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 over a weekendPHPBut 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:
$ Action = isset ($ _ REQUEST ['action'])? $ _ 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:
- define('yourPage',1);
Then, other pages should include:
- 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:
- public function dbExec($query)
- {
- $result = $this->db->exec($query);
- if (PEAR::isError($result)) errorRedirect($result->getMessage(), true);
- else return $result;
- }
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:
- // checks if arguments given are integer values not less than 0 - has multiple arguments function sanitizeInput() { $numargs = func_num_args();
- $arg_list = func_get_args();
- for ($i = 0; $i < $numargs; $i++)
- {
- if (!is_numeric($arg_list[$i]) || $arg_list[$i] < 0) errorRedirect("Unexpected variable value", true);
- }
- }
-
8. a php file is used to process input and a class. php file is used to process specific functions.
An important way to confuse code 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:
- require_once PROJECTROOT.'libs/messages.class.php';
- $message = new Message();
- switch ($action) { case 'display': $message->display();
- break;
- ...
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.
- private function getSentMessages($id)
- { $this->util->sanitizeInput($id);
- $pm_table = $GLOBALS['config']['privateMsg'];
- $users = $GLOBALS['config']['users'];
- $sql = "SELECT PM.*, USR.username as name_sender FROM $pm_table PM,
- $users USR WHERE id_sender = '$id' AND sender_purge = FALSE AND USR.id = PM.id_receiver AND is_read = TRUE ORDER BY date_sent DESC";
- $result = $this->dbQueryAll($sql);
- return $result;
- }
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:
- function smartyObject()
- {
- if ($GLOBALS['config']['SmartyObj'] == 0)
- {
- $smarty = new SmartyGame();
- $GLOBALS['config']['SmartyObj'] = $smarty;
- }
- else $smarty = $GLOBALS['config']['SmartyObj'];
- return $smarty;
- }
-
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.