10 things not to do in PHP7
1. Do not use the Mysql_ function
This day has finally come, and since then you are not only "shouldn't" use the Mysql_ function. PHP 7 has removed all of them from the core, meaning you need to migrate to a much better mysqli_ function, or a more flexible PDO implementation.
2. Do not write junk code
This may be easy to understand, but it becomes more and more important, as the speed increase of PHP 7 May hide some of your problems. Don't just be content with your site speed, because migrating to PHP 7 makes it faster.
To understand how fast is important and how to do things better, take a look at our article Speed Optimization Getting Started guide.
As a developer, you should always be sure to load scripts on demand, connect them as much as possible, write efficient database queries, use caches as much as possible, and more.
3. Do not use the PHP closing tag at the end of the file
You can see that when a file ends in PHP code, most of WordPress's core code removes the PHP tag at the end. In fact, the Zend framework specifically prohibits it. PHP does not need a closed tag at the end of the file, and we can eliminate it to ensure that no whitespace is added later.
4. Do not make unnecessary references to pass
I personally do not like the reference pass. I know that sometimes it's useful, but in other cases it makes the code difficult to understand and it's harder to predict the results.
It is said that some people think it makes the code run faster, but according to some senior PHP programmers, this is not true.
An example of why a reference is not good is that PHP is built with shuffle () and sort (). They modify the original array instead of returning the processed array, which is illogical.
5. Do not execute queries in loops
Executing a query in a loop is a waste of time. It imposes undue pressure on your system and may be able to get the same results faster outside of the loop. When I meet a situation that requires this, I usually use two separate queries to solve the problem, and I use them to build arrays of data. I will then iterate through the array and do not need to execute the query in the process.
Since WordPress works here, it may have some exceptions. Although Get_post_meta () gets a lot of data from the database, you can use it in a loop if you are traversing the metadata of a particular blog post. This is because when you first call it, WordPress actually gets all the metadata and caches them. Subsequent calls use these cached data without a call to the database.
The best way to understand this is to read the function document and use a tool similar to Query Monitor.
6. Do not use * in SQL queries
Of course, this is more like MySQL, but we're used to writing SQL code in PHP, so it's pretty much the same thing. In any case, do not use wildcards in SQL queries, especially when the database has many columns, if you can avoid them.
You should explicitly specify which rows are needed and just get them. This helps reduce the resources used, protect the data, and make things as clear as possible.
For SQL, you need to know all the available functions and test their speed as much as possible. Use SQL functions instead of PHP functions when calculating mean, sum, or similar values. If you are unsure of the speed of a query, test it and try some other compilation-then use the best one.
7. Do not trust user input
It is unwise to trust user input. Always verify, filter, Escape, check, and leave a good retreat. There are three issues with user data: We developers do not consider each possibility, it is usually incorrect, and it may be vandalism.
These threats can be protected by carefully considered systems. Be sure to check for the appropriate values using the built-in function like Filter_var () and Escape (or Precompile) when the database is processed.
WordPress has some functions to solve the problem. See the article verifying, 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 seconds from each page by shortening anything into a variable of one word, using multiple layers of ternary logic, and other means. But it will only cause you and the people around you a big problem.
Proper naming of variables, writing documents for code, preference for clarity rather than brevity. Even better, using standard object-oriented code, which itself is more or less a document, does not require a lot of inline values.
9. Don't reinvent the wheel
PHP has been going on for a long time now, and the site has been built for a long time. It is likely that no matter what you need to build, some people have already made it before. Don't be afraid to seek support from others, GitHub is your good friend, composer is also, Packagist also.
From the log tool to the Palette tool, from the Performance Analyzer to the unit test framework, from the Mailchimp API to the Twitter Bootstrap, everything can be obtained by pressing the key (or by tapping the command), using them!
10. Do not ignore other languages
If you're a PHP programmer, there's a good chance to know at least HTML, CSS, JavaScript, and MySQL. When you are better able to deal with these languages, it is time to re-learn JavaScript. JavaScript is not jQuery, you should reasonably learn JavaScript to use it more efficiently.
I also want to recommend to you to learn object-oriented PHP, which can save time and get better when the code is larger. For languages like C # and Java, they are easier to understand after you understand OOP.
Expand your knowledge by understanding Package Manager, build scripts, Coffeescript, less, SASS, YAML, scripting engines, and other powerful tools. I strongly recommend to you a look at other frameworks, especially Laravel.
How do you learn Ruby, RoR, Android, IPhone, and Windows Phone app development when you're using them to do a great job? You might think it's pointless because they're outside your comfort zone and your job needs, but that's what they mean. Each language has some practical things to learn, and new knowledge that has never been encountered. It is no coincidence that all top PHP developers know a lot of other programming languages.
10 things not to do in PHP7