30 PHP Best Practices for beginners (wilderness without lights) _php Tutorial

Source: Internet
Author: User
Tags coding standards php framework netbeans
1, and PHP manuals become good friends
2, open error Reporting
Error reporting is helpful in PHP development. You can find errors in your code that you didn't find previously, because not all bugs would make the program work. When the product is officially used, it is necessary to turn off the error report, otherwise the customer sees a bunch of strange characters don't know what that means.
3, using the IDE
The IDE (Integrated development environment, Integrated development environments) is a useful tool for developers.
The Wilderness recommends NetBeans IDE here.
4. Try using a PHP framework
5. Learning Dry Methods
DRY represents Don ' t Repeat yourself, which is a valuable programming concept, no matter what the language. Dry programming, as the name implies, is to make sure you don't write extra code.
6. Use space indent code to improve readability
7. "Tier" Your Code
Layering your application into different parts of the code. This makes it easy for you to change your code in the future. such as the usual MVC pattern.
8. Always use
9. Use meaningful, consistent naming conventions
10. Comments, comments, comments
11. Installing Mamp/wamp
12. Limit the run time to your scripts
PHP scripts typically run for 30 seconds, and PHP will throw a fatal error over this time.
13. Using OOP
14. Know the difference between double quotes and single quotes
15. Do not put phpinfo () in the root directory of the website
16. Never trust your users
17. Encrypt stored passwords
Rebuttal:
Keep in mind, however, that MD5 hashes has a long since been compromised. They ' re absolutely more secure than no, but, with the use of a enormous "rainbow table," Hackers can cross reference R Hash. To add even + security, consider adding a salt as well. A Salt is basically A additional set of characters so append to the user ' s string.
18. Using the Visual Database design tool
such as Dbdesigner and MySQL Workbench
19. Using Output buffering
Rebuttal:though not required, it's generally considered to is a good practice to go ahead and append the "Ob_end_flush (); function as well to the bottom of the document. P.S. Want to compress the HTML as well? Simply Replace "Ob_start ();" with "Ob_start (' Ob_gzhandler ')";
Refer to the Dev-tips article for more information.
Copy CodeThe code is as follows:





<title>Untitled</title>







20. Protect your code against SQL injections
Copy CodeThe code is as follows:
$username = mysql_real_escape_string ($GET [' username ']);
$id = $_get[' id '];
$statement = $connection->prepare ("select * from tbl_members WHERE id =?");
$statement->bind_param ("i", $id);
$statement->execute ();

By using prepared statements, we never embed the user's inputted data directly into our query. Instead, we use the ' Bind_param ' method to bind the values (and escaping) to the query. Much safer, and, notably, faster when executing multiple CRUD statements at once.
21. Try Orm (Object Relational mapping)
ORM Libraries for PHP like Propel, and ORM are built into PHP frameworks like CakePHP.
22. Cache database-driven pages
Such as:
Copy CodeThe code is as follows:
TOP of your script
$cachefile = ' cache/'. basename ($_server[' Script_uri ');
$cachetime = 120 * 60; 2 hours
Serve from the cache if it is younger than $cachetime
if (file_exists ($cachefile) && (Time ()-$cachetime < Filemtime ($cachefile))) {
Include ($cachefile);
echo " ";
Exit
}
Ob_start (); Start the output buffer
Your normal PHP script and HTML content here
BOTTOM of your script
$fp = fopen ($cachefile, ' w '); Open the cache file for writing
Fwrite ($FP, ob_get_contents ()); Save the contents of output buffer to the file
Fclose ($FP); Close the file
Ob_end_flush (); Send the output to the browser

23. Using the cache system
    • Memcached
    • Apc
    • XCache
    • Zend Cache
    • Eaccelerator
24. Validating cookie Data
Cookie data, like any data passed on the Web, can be harmful. You can validate cookies data with either the Htmlspecialchars () or mysql_real_escape_string ().
25. Using a static file caching system
As Smarty is a powerful templating system with a built-in cache.
26. Analyze Your code
Profiling your code with a tool like Xdebug can help you to quickly spot bottlenecks and other potential problems in your PHP code. Some IDEs like Netbeans has PHP profiling capabilities as well.
27. Coding standards
such as the Pear standard.
Keep Functions Outside of Loops
You take a hits of performance when you include functions inside of loops. The larger the loop and the longer the execution time would take. Take the extra time and line of code and place the function outside of the loop.
Editor's Note:think of it this. Try to remove as many operations from the loop as possible. Do you really need to create this variable for every iteration of the loop? Does really need to create the function each time? Of course not.
29. Do not copy non-additional variables (in fact this article is questionable, see the following instructions)
Such as:
Copy CodeThe code is as follows:
$description = strip_tags ($_post[' description ');
Echo $description;

Can be written as follows:
echo strip_tags ($_post[' description ');
Rebuttal:in reference to the comment on "doubling the memory," this actually is a common misconception. PHP implements "Copy-on-write" memory management. This basically means so can assign a value to as many variables as you like without have to worry about the data a Ctually being copied. While it's arguable that the "good" example exemplified above might do for cleaner code, I highly doubt that it's any Qu Icker.
In other words, PHP implements the "Copy-on-write" memory management method, the first code above does not have the situation that consumes double memory. So rebuttal seriously suspects that the second way of code is really faster than the previous one.
30. Update to the latest version of PHP
31. Reduce the number of database queries
32. Ask questions bravely
Like StackOverflow and so on is a good place.

http://www.bkjia.com/PHPjc/324069.html www.bkjia.com true http://www.bkjia.com/PHPjc/324069.html techarticle 1, and PHP manuals become good Friends 2, open the error Reporting error Reporting in PHP development is very helpful. You can find in your code the errors you didn't find earlier, because ...

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