Top Ten tips for PHP developers to _php tutorial

Source: Internet
Author: User
Tags php template how to use sql
1, how to correctly create a websiteThe index page
When you create each site, the index page of the site is one of the first things to do. If you are a PHP Novice, the typical practice when writing index pages 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. Use the request Global array to crawl Data
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, in most cases parsingThe more efficient code for data is generally as follows:
$action = Isset ($_request[' action ')? $_request[' action ']: 0; 3. Debugging PHP code with Var_dump
If you are looking for PHP debugging Technology, 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, SmartyHandling the presentation layer
Smarty is a PHP-written TemplatesPHP template engine, is currently one of the industry's most famous PHP template engine. 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. In brief, the purpose is to make PHP ProgrammerSeparated from the front-end personnel to enable programChange 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 is particularly important in the multi-person cooperation project.
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. For Database TutorialsTable or database connection information using global values 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 ', 1); Then, the other pages should contain:
if (!defined (' yourpage ')) die (' Access Denied '); This is done to prevent direct access to your other PHP pages. This way, any attempt to access the other by index.php Web page, you 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 function。 The sample code is as follows:
Public Function Dbexec ($query) {$result = $this->db->exec ($query); PEAR::iserror ($result)) Errorredirect ($result->getmessage (), true); else return $result; } This functionReceive only one Enquiry Statementand execute on 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 () {$numar GS = 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 V Ariable 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 '; $message = new Message (); Switch ($action) {case ' display ': $message->display (); 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. Know your SQLstatement, 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 related tables and more AdvancedTechnology. 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 '] [' Smar Tyobj '] = $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.


http://www.bkjia.com/PHPjc/444959.html www.bkjia.com true http://www.bkjia.com/PHPjc/444959.html techarticle 1, how to correctly create a site index page when creating each site, the establishment of the index page of the site is one of the first things to do. If you are a novice php, in writing I ...

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