Table of Contents
1. Do not use mysql_ class functions
2. Don’t write useless code
3. Do not use the PHP closing tag at the end of the file
4. Don’t pass parameters by reference unless necessary
5. Don’t use queries in loops
6. Don't use *
7. Don’t trust user input
8. Don’t be smart
9. Don’t reinvent the wheel
10. Don’t overlook other languages
Home Backend Development PHP7 Do you know what pitfalls PHP7 needs to pay attention to?

Do you know what pitfalls PHP7 needs to pay attention to?

Sep 25, 2021 pm 05:21 PM
php7

1. Do not use mysql_ class functions

Finally, you no longer need to see the prompts suggesting not to use mysql_ functions. Since PHP 7 completely removes them from the core, this means moving to better mysqli_-like functions, or a more flexible PDO layer.

2. Don’t write useless code

This seems like a brainless suggestion, but as the speed of PHP7 increases, it masks some problems and makes it increasingly important. Don’t be complacent just because switching to PHP7 makes your site faster.

To understand the importance of speed and how to do it better, check out our article Beginner’s Guide to Acceleration Optimization.

As a developer, you should ensure that scripts are loaded on demand, combined when possible, write efficient database queries, use caching if possible, etc.

3. Do not use the PHP closing tag at the end of the file

If you take a casual look, you will find that most WordPress core code files omit the PHP closing tag at the end. In fact, Zend Framework specifically disables closing tags. It is not required by PHP, omitting it at the end of the file ensures no extra whitespace at the end.

4. Don’t pass parameters by reference unless necessary

I personally don’t like passing parameters by reference very much. I certainly know that it can be useful in some situations, but most of the time it makes the code difficult to understand, difficult to follow, and difficult to predict the results.

People think that references make their code faster, but as this article from The Respectable PHP Programmer points out, that's not the case.

PHP’s built-in shuffle() or sort() function is a bad case of parameter passing by reference. It modifies the original array instead of returning a shuffled or sorted array, which is completely against our wishes.

5. Don’t use queries in loops

The worst thing is to use database queries in loops. It will put unnecessary stress on the system, and most likely, you can get the same results faster by using the query outside the loop. When I encounter a situation where I have to use it this way, I usually solve it by splitting it into two queries to construct an array. Then loop over the array without looping the query.

There may be some exceptions to this due to the way WordPress operates. get_post_meta() will get a metadata from the database, which you can use in a loop if you are looping through the metadata for a specific post. This is because WordPress actually takes all the metadata and caches it when you first use it. Subsequent calls actually call cached data rather than calling the database.

The best way to solve these problems is to read the function documentation and use something like a query listener.

6. Don't use *

in SQL queries. Well, this is more of a MySQL question, but we prefer to write SQL statements in code, so I say this is a Fair game. In any case, if you can avoid using wildcards, don't use them, especially if your database has many fields.

Explicitly specify the fields you need and retrieve only those fields. This helps save memory, protect data, and make things clearer.

On the SQL side, learn as much as possible about the functions available to you and test the speed. When calculating averages, sums, and calculating similar numbers, use SQL built-in functions instead of PHP functions. If you're not sure how fast a query is, test it and compare it with other approaches to choose the best one.

7. Don’t trust user input

It’s not wise to trust user input. For user input, there is always a need to filter, sanitize, escape, validate, and use fallbacks. There are three problems with user input: it is impossible for us developers to consider all possibilities, frequent mistakes, and intentionally malicious input.

A well-thought-out system can prevent all of these problems. When using a database, be sure to use built-in functions such as filter_var() to check validity, escape, and do whatever else you can.

WordPress has a bunch of functions to help you. Have a look at this article to learn more about Validating, escaping and sanitising user data.

8. Don’t be smart

Your goal is to write elegant code that clearly expresses your wishes. You may save 0.01 seconds on each page's loading time by shortening variable names, using multi-level ternary logic operations, and other tricks, but it's not worth the loss compared to the consequences of causing you and your team headaches and difficulty in maintaining.

Name variables appropriately and write code documentation in a concise and clear way. It's better to use a standardized object-oriented coding style and more or less document it, rather than using lots of inline code comments.

9. Don’t reinvent the wheel

PHP has been around for a while, and website development has been around even longer. Whatever you have done, someone else has done it before. Don’t be afraid to rely on others for support. Github, Composer, and Packagist are all your mentors.

From logging to color processors, from profilers to unit testing frameworks, from Mailchimp APIs to Twitter Bootstrap, everything is just a click of a button (or a command away) away, so use them !

10. Don’t overlook other languages

If you are a PHPer, it is now standard practice to know at least HTML, CSS, Javascript and MySQL. When you can handle these languages ​​well, it's time to learn Javascript again. Javascript is not jQuery. You should learn Javascript to utilize jQuery effectively.

I also recommend learning everything object-oriented in PHP. It's a lifesaver and will improve your code by orders of magnitude. It can also open doors to languages ​​like C# and Java, which can make it easier to understand object-oriented programming (OOP) once you have experience with them.

Expand your knowledge by learning package management, build scripts, Coffeescript, LESS, SASS, YAML, template engines, and other useful tools. I also wholeheartedly recommend looking at other frameworks, especially Laravel.

When you are good enough at these, consider Ruby, Ruby on Rails and app development for Android, iPhone, and Windows Phone? You may think these are pointless because they are outside of your comfort zone and job requirements, but they are exactly the point. Every language has some useful pedagogical knowledge and some harmless extras. All top PHP developers know other programming languages, this is no accident!

Recommended study: "PHP7 Tutorial"

The above is the detailed content of Do you know what pitfalls PHP7 needs to pay attention to?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
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

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What should I do if the plug-in is installed in php7.0 but it still shows that it is not installed? What should I do if the plug-in is installed in php7.0 but it still shows that it is not installed? Apr 02, 2024 pm 07:39 PM

To resolve the plugin not showing installed issue in PHP 7.0: Check the plugin configuration and enable the plugin. Restart PHP to apply configuration changes. Check the plugin file permissions to make sure they are correct. Install missing dependencies to ensure the plugin functions properly. If all other steps fail, rebuild PHP. Other possible causes include incompatible plugin versions, loading the wrong version, or PHP configuration issues.

How to solve the problem when php7 detects that the tcp port is not working How to solve the problem when php7 detects that the tcp port is not working Mar 22, 2023 am 09:30 AM

In php5, we can use the fsockopen() function to detect the TCP port. This function can be used to open a network connection and perform some network communication. But in php7, the fsockopen() function may encounter some problems, such as being unable to open the port, unable to connect to the server, etc. In order to solve this problem, we can use the socket_create() function and socket_connect() function to detect the TCP port.

How to install mongo extension in php7.0 How to install mongo extension in php7.0 Nov 21, 2022 am 10:25 AM

How to install the mongo extension in php7.0: 1. Create the mongodb user group and user; 2. Download the mongodb source code package and place the source code package in the "/usr/local/src/" directory; 3. Enter "src/" directory; 4. Unzip the source code package; 5. Create the mongodb file directory; 6. Copy the files to the "mongodb/" directory; 7. Create the mongodb configuration file and modify the configuration.

How to install and deploy php7.0 How to install and deploy php7.0 Nov 30, 2022 am 09:56 AM

How to install and deploy php7.0: 1. Go to the PHP official website to download the installation version corresponding to the local system; 2. Extract the downloaded zip file to the specified directory; 3. Open the command line window and go to the "E:\php7" directory Just run the "php -v" command.

PHP Server Environment FAQ Guide: Quickly Solve Common Problems PHP Server Environment FAQ Guide: Quickly Solve Common Problems Apr 09, 2024 pm 01:33 PM

Common solutions for PHP server environments include ensuring that the correct PHP version is installed and that relevant files have been copied to the module directory. Disable SELinux temporarily or permanently. Check and configure PHP.ini to ensure that necessary extensions have been added and set up correctly. Start or restart the PHP-FPM service. Check the DNS settings for resolution issues.

How to automatically set permissions of unixsocket after system restart? How to automatically set permissions of unixsocket after system restart? Mar 31, 2025 pm 11:54 PM

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

Which one is better, php8 or php7? Which one is better, php8 or php7? Nov 16, 2023 pm 03:09 PM

Compared with PHP7, PHP8 has some advantages and improvements in terms of performance, new features and syntax improvements, type system, error handling and extensions. However, choosing which version to use depends on your specific needs and project circumstances. Detailed introduction: 1. Performance improvement, PHP8 introduces the Just-in-Time (JIT) compiler, which can improve the execution speed of the code; 2. New features and syntax improvements, PHP8 supports the declaration of named parameters and optional parameters, making functions Calling is more flexible; anonymous classes, type declarations of properties, etc. are introduced.

Why does an error occur when installing an extension using PECL in a Docker environment? How to solve it? Why does an error occur when installing an extension using PECL in a Docker environment? How to solve it? Apr 01, 2025 pm 03:06 PM

Causes and solutions for errors when using PECL to install extensions in Docker environment When using Docker environment, we often encounter some headaches...

See all articles