Experience sharing: Tips for PHP developers _php Tutorials

Source: Internet
Author: User
Tags php language learn php php template vars how to use sql
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:

 
  
  
  1. $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:

 
  
  
  1. define (' yourpage '

Then, the other pages should contain:

 
  
  
  1. 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:

 
  
  
  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 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:

 
 
  1. //Checks if arguments given is 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 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:

 
  
  
  1. require_once Projectroot. ' libs/messages.class.php ' ;
  2. $messagenew message ();
  3. Switch ($action)
  4. {
  5. case 'display ':
  6. $message ->display ();
  7. 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.

 
 
  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 (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:

 
 
  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 (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 ...

  • 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.