Top Ten tips for PHP developers to _php tips

Source: Internet
Author: User
Tags php language learn php php programming php template how to use sql
What happens if you use a big mirror as a surfboard? Maybe 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 applies to PHP programming, although the analogy sounds odd. We often hear people trying to learn PHP with more time a weekend, but with all due respect, this is a very bad way to learn this programming language.

Why is it that the process of learning PHP is different from any other language?
By its very nature, if you have the means to "do things" in the PHP language, you will be handy when you use it, so it is worth your effort to understand these ways. In PHP, simply follow your own ideas to solve the problem is often a wrong way. It's not because you're a bad programmer, but because if you want to write good, maintainable code, there are some standard techniques you have to use. Now 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 creating each site, building the index page of the site is one of the first things to do. If you're a novice in PHP, the typical way to write the index page is to program only the content that 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, which is used by many websites.

2. Use request Global array to crawl data
In fact, there's no reason to use $_get and $_post arrays to crawl values. $_request This global array allows you to obtain a Get or form request. Therefore, in most cases, the more efficient code for parsing data is generally as follows:
$action = Isset ($_request[' action ')? $_request[' action ': 0;

3. Use Var_dump to debug PHP code
If you're looking for PHP debugging techniques, I have to say that var_dump should be the target you're looking for. This command can meet all your needs in the display of PHP information. And most of the debugging code is about 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, using PHP as a template for PHP templates. It separates the logical code from the external content, provides an easy to manage and use method to separate the PHP code from the original HTML code. Simply speaking, the goal is to make the PHP programmer with the front-end staff separation, so that the programmer changes the logic of the program will not affect the front-end staff of the page design, the front-end staff to modify the page does not affect the program logic, which in many people cooperation project is particularly important.

5. Create a config file when you really need to use global values
It's a bad idea to create global values at every turn, but sometimes the real thing really needs to be done. It's a good idea to use global values for database tables or database connection information, but don't use global values frequently in your PHP code. In addition, a better approach is to keep your global variables in a config.php file.

6, if not defined, prohibit access!
If you create the page correctly, no one else has reason to visit index.php pages outside of index.php or home.php. Once index.php is accessed, you can open the page you want by getting the variable. Your index page should contain similar following code:
Define (' Yourpage ', 1);
The other pages should then 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 working on database programming (a very common task in PHP), a good idea is to create a database class to handle any database management functionality. The sample code is as follows:
Copy Code code 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 only one 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:
Copy Code code as follows:

Checks if arguments given are integer values not less than 0-has multiple
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 confusing is to get user input and redirect it to another function for processing. The principle is very simple, the php file gets any input we need, and then redirects it to a function in the class file. For example, suppose you have a URL that resembles "Index.php?page=profile&action=display". by profile.php to retrieve the URL and get the operation is "display". Then using a simple switch function, let's perform the actual display function:
Copy Code code as follows:

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 a switch check. $message is just an object that is used by the calling function in the class.

9. Understand your SQL statements and always review them (Sanitize)
As I mentioned before, 99% of the most important parts of any PHP site are probably databases. Therefore, you need to be very familiar with how to use SQL correctly. Learn to correlate tables and more advanced techniques. I'll show you a sample function using MySQL and review it using the 7th function of this article.
Copy Code code as follows:

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 we execute our SQL command. Note the use of SQL here. You need to know how to use aliases and associated tables.

10, when you need only one object, use the single case mode
In a fairly common scenario in PHP, we only need to create an object once and then use it throughout our program. A good example is the Smarty variable, which can be used everywhere once it is initialized. A good implementation of this scenario is a single example pattern. The sample code is as follows:
Copy Code code 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 in this example), 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.

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.