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

Source: Internet
Author: User
Tags join php and php programming php template php website how to use sql

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 tutorial 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 prevents direct Access to 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 only receives and executes a 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. Input is processed in a php file, A class. php file processing 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:
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 in config. php initialization). If the 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.

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.