In the world of WEB development, PHP is one of the most popular languages. From PHP, you can easily find the scripts you need. Unfortunately, few people will use the "best How to write a PHP program. Here, we introduce to you 10 best practices of PHP. Of course, each of them is proven by masters.
1. Use PHP when appropriate - Rasmus Lerdorf
No one understands where PHP should be used more rationally than Rasmus Lerdorf, the creator of PHP. The language PHP was released in 1995. Since then, PHP has been like a prairie fire, burning through the entire development camp and changing the world of the Internet. However, Rasmus did not create PHP for this reason. PHP was born to solve the practical problems of WEB developers.
Like many open source projects, PHP has become popular. The motivation for its popularity cannot be explained by normal philosophy, and it is even so popular that it is somewhat self-admiring. It can be used as a case, a case caused by the need for tools to solve various Web problems. Therefore, when PHP first appeared, all such tool needs were focused on PHP.
However, you cannot expect that PHP can solve all problems. Lerdorf is the first to admit that PHP is just a tool, and there are many situations where PHP is beyond its capabilities.
Choose the right tool according to the job. I have gone to many companies to convince them to deploy and use PHP, but this does not mean that PHP is suitable for every problem. It is just a front-end scripting language that can solve most problems.
As a web developer, trying to solve every problem with PHP is unscientific and will waste your time. When PHP doesn't work, don't hesitate to try other languages.
2. Use multi-table storage to improve scalability – Matt Mullenweg The most popular blogging system (supported by a strong community): WordPress. After creating WordPress, Matt and his team launched the WordPress.com platform, a free blog site based on WordPress MU. Today, WordPress.com has approximately 4 million users, who contribute more than 140,000 posts every day. (To see more WordPress.com statistics, click here.)
If anyone knows how to make a website scale easily, it’s Matt Mullenweg. In 2006, Matt made forward-looking improvements to WordPress's data structure and explained why WordPress MU uses independent MYSQL tables for each blog instead of stuffing all blog data into one huge table. We tested this approach, but found it too expensive to scale. If you use an overall data structure, you will face server hardware problems in the face of large traffic. Inside MU. Users are distributed into separate tables and can be easily organized. For example, WordPress.com stores user data in 4,096 databases. These databases can disperse large-scale data access and divert traffic and pressure. The portability of data tables allows the code (blog) to run faster and makes the system more scalable. Relying on powerful caching strategies and flexible database utilization strategies, Matt showed people that the most popular Facebook and WordPress.com can run stably under PHP and handle an astonishing amount of traffic.3. Never trust the user – Dave Child
Dave Child is the heart of Added Bytes (previously ilovejackdaniels.com), a website known for his excellent Famous for "cheat sheets for many programming languages". Dave works for a number of UK companies and has established himself as an authority in the programming world.
Dave has provided a lot of thoughtful advice for PHP developers, summarized in "writing secure code in PHP": Never trust your users, they may even hurt you. There is a basic principle of web development that I cannot repeat enough times: never trust your users and assume that every unit of data on your website is collected from the users. malicious code. Many times, you must use JAVAscript to verify the content submitted by the form on the client. If you are used to this, then this is a good habit. If security is important to you, this is the most important principle to learn. Dave is currently working on compiling examples for his "Writing Secure PHP" series of books. At the end of the book, he said: Finally, become a little paranoid. Unless you think your site will never be attacked, face the problem head on because when it does happen, you're going to be in a bad situation. You need to regard every user as a hacker who will bring about an attack and defense on the site, do everything possible to protect the security of the site, and at the same time think of solutions to the corresponding problems.4. Use PHP caching more – Ben Balbo
Ben Balbo develops Site Point, a website that provides guidance to developers and designers. He is a member of the Melbourne PHP Development and Open Source Club, so he has a certain understanding of PHP and has certain ideas and experience in PHP caching.
If you have a site that has a lot of traffic but is not updated frequently (such as a blog, based on some kind of CMS), maybe it needs some modifications. These modifications will not take too much time, but Makes an outstanding contribution to performance. If you want to establish a caching mechanism for a complex/fast-updated site, the process may be tortuous, but the benefits are obvious.
There are many PHP caching technologies. Ben recommended the following ones for us:
◆The running results of the cache function
◆Set the expiration time
◆ Cache files downloaded by IE
◆Template caching technology
◆Cache_Lite
Due to the characteristics of PHP as a dynamic language, the caching mechanism is very important for sites that do not update frequently .
5. Accelerate PHP development using IDE, Templates and Snippets – Chad Kieffer
When Chad Kieffer takes a break from UI design and database optimization, he Will share a lot of technical experience on his blog 2 tablespoons. Due to Chad's comprehensive development in many aspects, he can often find problems that other programmers cannot find and form relevant experience, especially his method of developing websites. He is involved in every aspect of website development, so his advice is very useful in improving the big picture of website development.
Chad believes that using an IDE like Eclipse PDT (Eclipse’s PHP development package) and using some template technologies and open source projects can effectively improve the development speed of PHP.
Compact plans, long to do lists and deadlines make developers very frustrated. However, some features, such as Eclipse Templates, can effectively reduce coding time and the chance of errors.
Generally speaking, any project can be automated. The higher the degree of automation, the shorter the time it will take you to complete the project. Taking the time to develop frameworks and templates that will be used frequently will save you even more time later. At the same time, using an IDE like Eclipse and the PDT package, you will find that your efficiency is significantly improved. The IDE can automatically close, complete semicolons and debug locally.
6. Make good use of PHP filter functions – Joey Sochacki
Maybe Joey Sochacki is not as famous as Matt Mullenweg, but he is also an experienced developer. And he shared a lot of technical experience through his blog Devolio
Joey found that there are many places that need to be filtered in the process of writing PHP code, but not many coders pay attention to PHP's built-in filtering function.
Filtering data is something we often need to do, but many feature-rich PHP built-in filtering functions are unknown. Using PHP built-in functions like filter_*, we can handle almost all filtering tasks, including data type validation/URL/email and IP address validation/special character processing, etc.
Filtering is a complicated thing, but I believe joey's discovery will give you a lot of inspiration and let you realize the powerful filtering function of PHP.
7. Use PHP framework - Josh Sharp
There has been a lot of controversy about whether you should use Zend, CakePHP, Code Igniter, or other PHP frameworks, but in In the minds of web developers, they have their own standards of measurement.
Josh Sharp created a website that provides bread and butter services, so he has some experience in using PHP framework to develop websites. He believes that using a PHP framework for project development can effectively save time and reduce the chance of errors. Why? Because he feels that PHP is really easy to get started with.
The ease of use of PHP is sometimes flawed, because the loose syntax often leads to the creation of many erroneous codes. But if you use a PHP framework, the chance of errors will be greatly reduced.
The PHP framework can make your code structure more standardized and save a lot of time.
8. Don’t use PHP framework – Rasmus Lerdorf
Contrary to Josh’s point of view, Rasmus Lerdorf, the originator of PHP, thinks it’s best not to use PHP framework. Why? Because PHP that is not based on a framework performs better. In his speech at Drupalcon 2008, Rasmus used the "Hello World" example to compare the performance between some framework PHP and simple PHP. The results showed that the performance of framework PHP was far behind.
9. Using batch processing – Jack D. Herrington
Jack Herrington is no stranger to the PHP world and has contributed more than 30 articles to the famous IBM developerWorks. He also published the book "PHP Hacks", so he is a true expert.
Herrington recommends using batch processing and Cron to replace program scripts that can run in the background. Web users are not willing to wait for your processing online, so some things are better suited to be processed in the background.
Admittedly, this is a bit overkill in some cases, but you can clearly see that using Cron, MySQL, PHP object-oriented methods, and Pear::DB are convenient tools to create a batch Handling tools is not a complicated thing.
Jack believes that using cron, PHP and MySQL to handle some tasks in the background is much more cost-effective than multi-process business logic.
I have tried both methods, and I think Cron is very consistent with the "Keep It Simple, Stupid" (KISS) principle, which makes background processing simple. Compared with multi-process business logic, it has no risk of memory overflow. You can create a simple batch script and run it in cron. This script will regularly check whether there are tasks that need to be processed, and will automatically exit after processing, so you don't have to worry about whether a process is stuck or stuck in an infinite loop.
10. Enable error reporting in a timely manner – David Cummings
David Cummings has a company that specializes in providing CMS software services and has won several awards. He has a very rich experience PHP development experience. David once wrote "two PHP tips he wished he’d learned in the beginning", one of which is: enabling error reporting in time, which will save a lot of time.
I tell people that the most important thing is to maximize error reporting in PHP. Why? Because PHP may hide many small problems:
◆Variables are not predefined
◆Unavailable variables are referenced in code snippets
◆Undefined constants are used These factors may not seem like a big deal unless you are writing some class library using an object-oriented approach. Often, turning off error reporting will likely cost you more to maintain your code.
Error reports can help you easily find problems with your code. If the level of the error report is high enough, subtle errors can be discovered immediately, helping you save overall debugging time.