What happens if you use a big mirror as a surfboard? Perhaps you will conquer the waves in a short time, but you must know from the bottom of your heart that this is not the right choice for surfing. The same principle applies To
PHPProgramming, though this analogy sounds a bit odd. We often hear people trying to learn PHP for more than a weekend, but with all due respect, this is a very bad way to learn the programming language.
Why is the process of learning PHP different from any other language?
In essence, if you know how to "do things" in the PHP language, you'll be handy when you use it, so it's worth investing your energy to understand these ways. In PHP, it is often a mistake to simply follow your own thinking to solve a problem. It's not because you're a bad programmer, but because if you want to write good maintainable code, there are some standard tricks you have to use. Let's take a look at the 10 tips you need to know.
1. How to create the index page of a website correctly
When you create each site, the index page of the site is one of the first things to do. If you are a novice PHP, the typical way to write an index page is to program only what is needed for the index page, and other links to create another page. However, if you want to learn a more efficient way to implement PHP programming, you can use the "index.php?page=home" mode, many sites are using this model.
2. Fetch data using the request Global array
There is actually no reason to use $_get and $_post arrays to fetch values. $_request This global array allows you to get a Get or form request. Therefore, the more efficient code that parses the data in most cases is generally as follows:
- $action = isset ($_request[' action '$_ REQUEST[' action '
3. Debugging PHP code with Var_dump
If you are looking for PHP debugging techniques, I must say that var_dump should be the target you are looking for. This command will meet all your needs in terms of displaying PHP information. Most of the debugging code is related to getting the values in PHP.
4, PHP processing code logic, Smarty processing presentation layer
Smarty is one of the most famous PHP template engines in the industry, which is a PHP template engine written using PHP. It separates the logic code from the external content, provides an easy-to-manage and useful way to separate the PHP code that was originally mixed with HTML code.
To put it simply, the goal is to make the PHP programmer separate from the front-end staff, so that programmers change the logic content of the program does not affect the front-end staff of the page design, the front-end staff to re-modify the page does not affect the program logic, which in the multi-person cooperation project is particularly important.
5. When you do need to use global values, create a config file
It's a bad idea to create global values at will, but sometimes it does. Using global values for database tables or database connection information is a good idea, but do not use global values frequently in your PHP code. In addition, a better approach is to put your global variables in a config.php file.
6, if not defined, access is forbidden!
If you create the page correctly, then no one else has a reason to visit a index.php page other than index.php or home.php. Once index.php is accessed, you can open the desired page by obtaining a variable. Your index page should contain the following code similar to this:
- define (' yourpage '
Then, the other pages should contain:
- if (!defined ('yourpage 'die(' Access Denied ' )
This is done to prevent direct access to your other PHP pages. In this way, anyone who tries to access other pages without index.php will get a "access denied" message.
7. Create a Database class
If you are doing database programming (a very common task in PHP), a good idea is to create a database class to handle any database management functions. 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 a query statement and executes it. It also handles any errors that may occur. You can also include the audit code here, but I prefer to use a similar audit function:
- //Checks if arguments given is 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 processing input, a class.php file processing specific functions
One important way to keep your code from getting messy is to get user input and redirect it to other functions for processing. The principle is very simple, the php file gets whatever input we need, and then redirects it to a function in the class file. For example, suppose you have a URL similar to "Index.php?page=profile&action=display". The URL is retrieved by profile.php and the operation is "display". Then using a simple switch function, let's perform the actual display function:
- require_once Projectroot. ' libs/messages.class.php ' ;
- $messagenew message ();
- Switch ($action)
- {
- case 'display ':
- $message ->display ();
- Break ;
As shown above, I used a message class and started a switch check. $message is just an object that is used by the calling function in the class.
9. Understand your SQL statement and always review it (Sanitize)
As I mentioned before, 99% of the most important parts of any PHP Web site may be databases. Therefore, you need to be very familiar with how to use SQL correctly. Learn to correlate tables and more advanced technologies. I'll show you an example of a function that uses MySQL and review it using the 7th function in this article.
- 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 (passing the message ID through a get variable), and then we execute our SQL command. Note the use of SQL here. You need to know how to use aliases and association tables.
10. Use singleton mode when you only need one object
In a fairly common scenario in PHP, we only need to create an object once and then use it in our entire program. A good example of this is the Smarty variable, which can be used anywhere once it is initialized. A good implementation of this scenario is a singleton pattern. 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 (which is initialized in config.php), and if it has a value of 0, we will create a new Smarty object. Otherwise, it means that the object has been created and we just need to return it.
Hope the introduction of this article, can bring you help.
http://www.bkjia.com/PHPjc/445759.html www.bkjia.com true http://www.bkjia.com/PHPjc/445759.html techarticle What happens if you use a big mirror as a surfboard? Perhaps you will conquer the waves in a short time, but you must know from the bottom of your heart that this is not the right choice for surfing. With the same ...