In the Web development World, PHP is one of the most popular languages, from PHP, you can easily find the script you need, unfortunately, few people will use "best practices" to write a PHP program. Here, we introduce you to PHP's 10 best practices, of course, each of which has been proven by the Masters.
1. Use Php–rasmus Lerdorf at the right time
No one than the PHP creator Rasmus Lerdorf understand where PHP is more reasonable, he released the language of PHP in 1995, since then, PHP like a prairie fire, burning all over the development camp, changed the world of the Internet. However, Rasmus does not create PHP for this reason. PHP was born to solve the real problems of web developers.
Like many open source projects, PHP has become popular, and popular motives are not interpreted with normal philosophy, and even somewhat narcissistic. It can be used as a case, a case of a tool that solves a variety of web problems, so when PHP first appears, this tool needs to be focused on PHP.
However, you can't expect PHP to solve all the problems. Lerdorf is the first person to admit that PHP is just a tool, and PHP has a lot of things that the force can't.
Choose the right tool according to the different work. I ran a lot of companies to persuade them to deploy and use PHP, but that doesn't mean that PHP works for all problems. It just can be a front-end Footstep language that solves most of the problems.
As a web developer, trying to solve all of the problems with PHP is unscientific and will waste your time. When PHP is not playing, do not hesitate to try other languages.
2. Use multi-table storage to increase scale scalability –matt Mullenweg
No one wants to question Matt Mullenweg's authority in PHP, and he has developed the most popular blogging system on the planet, backed by a strong community power: WordPress. After creating WordPress, Matt and his team launched the WordPress.com platform, a free blog site based on WordPress MU. Now, WordPress.com already has about 4 million users, who provide more than 140,000 logs per day. (To see more wordpress.com statistics, please click here.)
If someone knows how to make the site scalable, this person must be Matt Mullenweg. In 2006, Matt made a forward-looking improvement to the WordPress data structure and explained why WordPress Mu used a separate MySQL table for each blog, rather than stuffing all the blog data into a huge form.
We tested this method, but found that if we were to extend its scalability it would be too expensive. If you use a whole data structure, in the face of large traffic, you will face the problem of server hardware. Inside the MU. Users are distributed in separate tables and can be easily organized. For example, WordPress.com stores the user's data in 4,096 databases, which can disperse large-scale data access and achieve traffic and pressure diversion.
The mobility of the data tables allows the Code (blog) to run faster, and the system to be more scalable. Relying on the powerful caching strategy and flexible database application strategy, Matt showed people that the most popular Facebook and wordpress.com are running stably under PHP and handling amazing traffic.
3. Never trust user –dave child
Dave Child is the core character of added Bytes (previously ilovejackdaniels.com) website with his excellent cheat sheets for many programming languages and famous. Dave serves a number of British companies and has established considerable authority in the programming world.
Dave provides a lot of thoughtful advice to PHP developers and concludes with writing secure code in PHP: Don't trust your users, they might even hurt you.
There is a basic principle of web development, I repeat many times do not feel enough, that is: do not trust your users, but also assume that every unit of data in your site is collected from users of malicious code. Most of the time, you have to use JavaScript to submit the content in the client inspection form, and if you get used to it, then it's a good habit. If security is important to you, this is the most important principle to learn.
Dave is currently working on an example for its Writing Secure PHP series of books, which he said at the end of the book:
Finally, become paranoid. Unless you think your site will never be attacked, look squarely at all the problems, and when the problem really happens, your situation will get very bad. You need to regard each user as a hacker who will bring a defensive station, and try every means to protect the security of the site, while thinking about the solution of the corresponding problem.
4. Use PHP cache –ben Balbo
Ben Balbo developed site Point, a website that provides guidance for 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 some ideas and experience with PHP caching.
If you have a large number of visits, but not frequent sites (such as blogs, based on some kind of CMS), perhaps it needs some transformation, these changes will not take too much time, but the performance has a prominent contribution. The process can be tortuous if you want to create a caching mechanism for a site with complex/updated frequencies, but the benefits are obvious.
There are many kinds of PHP caching techniques, and Ben recommends the following for us:
Running results of the cache function
Set Expiration Time
Cached files for IE downloads
Template Caching Technology
Cache_lite
Because of the nature of PHP as a dynamic language, the caching mechanism is very important for sites that are not updated at a very fast rate.
5. Use IDE, templates and snippets to accelerate PHP development –chad Kieffer
When Chad Kieffer from the UI design and database optimization work, he will share a lot of technical experience on his blog, 2 tablespoons. Due to the comprehensive development of Chad, he often discovers problems that other programmers cannot find, and develops relevant experience, especially the way he develops the website. He is involved in all aspects of website development, so his advice is very useful for improving the bigger picture of website development.
Chad believes that using Ides such as Eclipse PDT (Eclipse's PHP development package), while using some template technology and open source projects, can effectively improve the speed of PHP development.
The compact plan, long-to-do lists and deadlines make developers very depressed. However, some features, such as Eclipse Templates, can effectively reduce the time to encode and the chance of error.
In general, any project can be automated, the higher the degree of automation, the less time you have to complete the project. Taking the time to develop frameworks and templates with high frequency will save you more time later. At the same time, using an IDE like Eclipse and the PDT package, you will find that efficiency is significantly improved, the IDE can automatically close, complete the semicolon and can debug locally.
6. Use the filter function of PHP –joey Sochacki
Perhaps Joey Sochacki is not as famous as Matt Mullenweg, but he is also a seasoned developer and has shared a lot of technical experience through his blog Devolio.
Joey finds that there are many places to filter in the process of writing PHP code, but there's not much coder about PHP's built-in filtering functions.
Filtering data is something we often need to do, but many feature-rich PHP built-in filtering functions are not known. With 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 handling, and more.
Filtering is a complex thing, but I'm sure Joey's discovery will give you a lot of insight into PHP's powerful filtering capabilities.
7. Using the PHP framework –josh Sharp
There's been a lot of controversy about whether you should use Zend, CakePHP, Code igniter, or any other PHP framework, but in the minds of web developers, they have their own metrics.
Josh Sharp has created a website that provides bread and butter services, so he has some experience with using the PHP framework to develop a website. He thinks that using a PHP framework for project development (use a PHP framework) can save time and reduce the chance of error. Why? Because he thinks PHP is really good to get started.
PHP's ease-of-use is sometimes flawed, because not-so-strict syntax often leads to the emergence of many error codes. But if you use a PHP framework, the odds of error are greatly reduced.
PHP Framework can make your code structure more standardized, and save a lot of time.
8. Do not use the PHP framework –rasmus Lerdorf
Contrary to Josh's point of view, the originator of PHP Rasmus Lerdorf that it is best not to use the PHP framework, why? PHP performance is better because it is not framework-based. Rasmus in the Drupalcon 2008 speech, with "Hello world" example to compare some framework PHP and simple PHP performance, the results show that the framework PHP performance is far behind.
9. Using batch –jack D. Herrington
Jack Herrington is no stranger to the PHP world, and has contributed more than 30 monographs to the famous IBM Developerworks, and has published the book "PHP Hacks", so he is a real expert.
Herrington recommends using batch and cron instead of those that can run in the background, and web users are not willing to wait online for your processing, so there are things that are better for you to handle in the background.
Admittedly, in some cases this is a bit overqualified, but you can clearly see that using cron, MySQL, PHP Object-oriented methods and pear::D b These handy tools to create a batch tool are not a complex thing.
Jack thinks that using cron, PHP and MySQL can handle some tasks in the background, which is much more cost-effective than a multi-process business logic.
I've tried both methods, and I think Cron fits very well with the principle of "Keep it Simple, Stupid" (KISS), which makes background processing easier. It has no risk of memory overflow compared to multi-process business logic. You can create a simple batch script and run it in Cron, which periodically checks to see if a task needs to be processed, and then exits automatically after processing, so you don't have to worry about a process jam or a dead loop.
10. Timely enable error Reporting –david Cummings
David Cummings has a company dedicated to CMS software services, and has won several awards, and he has a very rich experience in PHP development. David has written "Two PHP tips he wished he learned in the beginning", one of which is: enabling error reporting in a timely manner can save a lot of time.
I tell people that the most important thing is to maximize the PHP error report, why? Because PHP can hide a lot of minor problems:
Variable not pre-defined
A variable that is not available is referenced in a code fragment
Using undefined constants These factors don't look like a big deal unless you're writing some class libraries using an object-oriented approach. In general, closing the error report will probably give you a greater cost to maintain your code.
Error reporting can help you easily find the problem with the code, and if the error report is high enough, subtle errors can be immediately discovered, helping you to save overall debug time.
http://www.bkjia.com/PHPjc/445644.html www.bkjia.com true http://www.bkjia.com/PHPjc/445644.html techarticle in the Web development World, PHP is one of the most popular languages, from PHP, you can easily find the script you need, unfortunately, very few people will use best practices to write a PHP program ...