search
HomeBackend DevelopmentPHP Tutorial10 top masters give guidance on PHP development_PHP Tutorial

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 "best practices" 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 to use PHP more rationally than Rasmus Lerdorf, the creator of PHP. He released the PHP language 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 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

No one wants to question Matt Mullenweg’s authority on PHP, he developed the planet The most popular blogging system on the Internet, (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.

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 core figure of the Added Bytes (previously ilovejackdaniels.com) website, which is known for his excellent Famous for his "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 user. malicious code. Many times, you have to 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 useful for sites that do not update frequently. important.

5. Use IDE, Templates and Snippets to accelerate PHP development - Chad Kieffer

When Chad Kieffer takes a break from UI design and database optimization, He shares many technical experiences 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 such as Eclipse PDT (Eclipse’s PHP development package) and using some template technologies and open source projects can effectively increase the speed of PHP development.

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 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 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 thinks PHP is really easy to use.

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, believes that it is 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. Use 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 specializes in PHP Hacks and has 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, in some cases, this is a bit overkill, but you can clearly see that using Cron, MySQL, PHP object-oriented methods, and Pear::DB are convenient tools to create a batch processing tool and 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 very Rich 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 turn on PHP's error reporting to the maximum extent, why? Because PHP can hide a lot of little problems:

  • Variables are not predefined
  • Referencing unavailable variables in code snippets

Using undefined constants 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.

I hope that through the introduction of the above content, you will have a new understanding of PHP development.


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/445765.htmlTechArticleIn 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 use best practices to write a PHP program...
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
The Continued Use of PHP: Reasons for Its EnduranceThe Continued Use of PHP: Reasons for Its EnduranceApr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python: Exploring Their Similarities and DifferencesPHP and Python: Exploring Their Similarities and DifferencesApr 19, 2025 am 12:21 AM

PHP and Python are both high-level programming languages ​​that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment