Ten pitfalls you need to avoid in PHP 7
This article shares with you ten pitfalls that you need to avoid in PHP 7. It has a certain reference value. Friends in need can refer to it
1. Do not use mysql_ class functions
Finally, you no longer need to see the prompts suggesting not to use mysql_
functions. Because PHP 7 completely removes them from the core, this means you should move 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, the 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 analyzers to unit testing frameworks, from Mailchimp APIs to Twitter Bootstrap, everything is available with the push of a button (or typing of a command), go 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!
Related recommendations:
Ten tips for using Redis correctly
Ten advanced tips for PHP_PHP programming
The above is the detailed content of Ten pitfalls you need to avoid in PHP 7. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

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 have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.
