10 things not to do in PHP 7, remember

1. Don't use mysql_ function
This day has finally come, and you should not only "should" use mysql_ function from then on. 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.

2. Don't write junk 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 will make it faster.

To understand how important speed is and how to make things better, take a look at our article Speed ​​Optimization Getting Started 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 whenever possible, and more.

3. Don't use the PHP closing tag at the end of the file.
You can take a look. When a file ends with PHP code, most of the core code of WordPress removes the end PHP tag. In fact, the Zend framework specifically forbids it. PHP does not need the closing tag at the end of the file, and we can remove it to ensure that no white space is added at the end.

4. Don't do unnecessary passing by
reference. Personally, I don't like passing by reference. I know that sometimes it is very practical, but in other cases it makes the code difficult to understand and more difficult to predict the results.

It is said that some people think that it makes the code run faster, but according to some senior PHP programmers, this is not correct.

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

5. Don't execute the query in the loop
Executing queries in a loop is very 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 this, I usually use two separate queries to solve the problem, and I use them to build the data array. I will iterate through the array afterwards, and there is no need to perform queries in the process.

Since WordPress works here, it may have some exceptions. Although get_post_meta() will fetch a lot of data from the database, if you are traversing the metadata of a particular post, you can use it in a loop. This is because when you call it for the first time, WordPress actually fetches all metadata and caches them. Subsequent calls use these cached data, no database calls.

The best way to understand this is to read the function documentation and use a tool like Query Monitor.

6. Don't use it in SQL queries*
Of course, this is more like a MySQL problem, but we are used to writing SQL code in PHP, so they are all 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 exactly which rows are needed, and only get them. This helps reduce resources used, protect data, and make things as clear as possible.

For SQL, you need to understand all available functions and test their speed as much as possible. When calculating averages, sums, or similar values, use SQL functions instead of PHP functions. If you are not sure about the speed of a query, test it and try some other compilations - use the best one later.

7. Don't trust user input
. It is unwise to trust user input. Always verify, filter, escape, check and leave a way out. There are three problems with user data: we developers do not consider every possibility, it is usually incorrect, and it may be sabotage.

A well-considered system can protect against these threats. Be sure to use a built-in function like filter_var() to check the appropriate value and escape (or pre-compile) when processing the database.

WordPress has some functions to solve the problem. For details, see the article Checking, Escaping and Filtering User Data.

8. Don't pretend to be smart.
Your goal should be to write elegant code to express your intentions more clearly. You may be able to optimize 0.01 second from each page by shortening anything to a variable of one word, using multiple layers of ternary logic, and other means. But this will only cause big trouble for you and the people around you.

Name variables reasonably, document the code, and choose clarity over conciseness. It can even be better, using standard object-oriented code, which is more or less a document itself and does not require a lot of inline values. For example: http://www.999sjw.com

9. Don’t reinvent the wheel
PHP. It’s been a long time since the website was created. It is possible that no matter what you need to make, some people have already made it before. Don't be afraid to ask others for support. Github is your best friend, so is Composer, and so is Packagist.

From logging tools to color correction tools, from performance analyzers to unit testing frameworks, from Mailchimp API to Twitter Bootstrap, everything can be accessed by pressing a button (or typing a command), use them!

10. Don't ignore other languages.
If you are a PHP programmer, now there is a good opportunity to learn at least HTML, CSS, JavaScript and MySQL. When you can handle these languages ​​better, it's time to relearn JavaScript. JavaScript is not jQuery, you should learn JavaScript reasonably to use it more efficiently.

I also plan to recommend to you to learn object-oriented PHP, it can save time, and will become better when the code size is larger. For languages ​​like C# and Java, after you understand OOP, they are also easier to understand.

Expand your knowledge by understanding package managers, build scripts, CoffeeScript, LESS, SASS, YAML, script engines and other powerful tools. I strongly recommend you to take a look at other frameworks, especially Laravel.

How about learning Ruby, RoR, Android, iPhone and Windows Phone application development when you use them to accomplish tasks well? You might think that this is meaningless because they are outside your comfort zone and work needs, but this is what they mean. Each language has some practical things to learn, as well as new knowledge that has never been encountered. It is no accident that all top PHP developers know many other programming languages.

Guess you like

Origin blog.csdn.net/hbznd/article/details/114367106