10 things not to do in PHP 7

Posted on

1. Don’t use MySQL_ function

The day has finally come when you are not only “not supposed” to use mysql_ Function. PHP 7 has removed all of them from the core, which means you need to migrate to a much better mysqli_ Function, or a more flexible PDO implementation.

10 things not to do in PHP 7


2. Don’t write garbage code

This one may be easy to understand, but it will become more and more important, because the speed increase of PHP 7 may hide some of your problems. Don’t just be satisfied with the speed of your site, because migrating to PHP 7 makes it faster.

To understand how important speed is and how to do things better, take a look at our article speed optimization guide.

As a developer, you should always make sure to load scripts on demand, connect to them as much as possible, write efficient database queries, use caching as much as possible, and so on.

10 things not to do in PHP 7


3. Don’t use PHP closing tag at the end of the file

You can see that when a file ends with PHP code, most of WordPress’s core code removes the PHP tag at the end. In fact, the Zend framework specifically bans it. PHP doesn’t need a closed label at the end of the file, and we can remove it to ensure that no white space is added.

10 things not to do in PHP 7


4. Don’t pass unnecessary references

I personally don’t like citation passing. I know that sometimes it’s useful, but in other cases it makes the code harder to understand and to predict results.

It’s said that some people think it makes code run faster, but according to some PHP advancedprogrammerThis is not true.

An example of why references are bad is that PHP built-in shuffle() and sort(). They modify the original array instead of returning the processed array, which is very illogical.

10 things not to do in PHP 7


5. Do not execute the query in the loop

Executing a query in a loop is wasteful. It puts unnecessary pressure on your system and may be able to get the same result faster outside the loop. When I encounter a situation where I need to do this, I usually use two separate queries to solve the problem, and I use them to build data arrays. After that, I’ll traverse the array, and I don’t need to execute the query in the process.


The best way to understand this is to read the function documentation and use tools like query monitor.

10 things not to do in PHP 7


6. Do not use in SQL query*

Of course, this is more like mysql, but we are used to writing SQL code in PHP, so it’s almost the same. In any case, if you can avoid it, don’t use wildcards in SQL queries, especially when the database has many columns.

You should specify which rows are required and just get them. This helps to reduce resources, protect data, and make things as clear as possible.

For SQL, you need to know all the functions available and test their speed as much as possible. When calculating means, sums, or similar values, use SQL functions instead of PHP functions. If you’re not sure about the speed of a query, test it and try some other compilation – then use the best one.

10 things not to do in PHP 7


7. Don’t trust user input

It’s unwise to trust user input. Always check, filter, escape, check and leave the way back. There are three problems with user data: we developers don’t consider every possibility, it’s usually incorrect, and it can be vandalism.

A well thought out system can protect against these threats. Be sure to use filters like this_ The built-in function of VaR () checks for appropriate values and escapes (or precompiles) when processing the database.

WordPress has functions to solve problems. For details, please refer to verification, escape and filtering of user data.



Don’t pretend to be smart

Your goal should be to write elegant code to express your intention more clearly. You may be able to optimize 0.01 seconds from each page by reducing anything to a single word variable, using multiple layers of ternary logic, and other means. But it will only cause you and the people around you big trouble.

Name variables reasonably, write documents for code, and give priority to clarity rather than brevity. It’s even better to use standard object-oriented code, which is more or less a document and doesn’t need a lot of inline values.



9. Don’t reinvent the wheel

PHP has been around for a long time, and websites have been built for a longer time. It’s very likely that whatever you need to build, some people built it long ago. Don’t be afraid to ask for support from others. GitHub is your good friend, so is composer and so is packgist.

From logging tool to palette tool, from performance analyzer to unit testing framework, from MailChimp API to twitterBootstrap, everything can be obtained by pressing the key (or typing the command), use them!



10. Don’t ignore other languages

If you’re a

I’m also going to recommend learning object-oriented PHP, which can save time and get better when the code is larger. For languages like c#and Java, after you know OOP, they are easier to understand.

Expand your knowledge by understanding package manager, build scripts, coffeescript, less, sass, yaml, script engine and other powerful tools. I strongly recommend taking a look at other frameworks, especially laravel.

10 things not to do in PHP 7

When you use them to do a good job, learn


Leave a Reply

Your email address will not be published.