Attention! 10 things not to do in PHP 7 _php instance

Source: Internet
Author: User
Tags closing tag php code mailchimp api zend framework

Remember, 10 things you don't do in PHP 7

1. Do not use the Mysql_ function

The day has finally come, and since then you should not only "not" use the Mysql_ function. PHP 7 has removed them all from the core, meaning that you need to migrate to a much better mysqli_ function, or a more flexible PDO implementation.

2. Do not write garbage code

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

To understand how important the speed is and how to do it better, please 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 caching as much as possible, and more.

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

You can take a look, when a file at the end of the PHP code, WordPress Most of the core code will be the end of the PHP tag removed. In fact, the Zend framework specifically forbids it. PHP does not need a closed tag at the end of the file, and we can make sure that we don't add any white space characters after that.

4. Do not make unnecessary reference to pass

I personally do not like reference delivery. I know that sometimes it's practical, but in other cases it makes the code difficult to understand, and it's harder to predict 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 quote is bad is that PHP built shuffle () and sort (). They modify the original array instead of returning the processed array, which is quite illogical.

5. Do not execute the query in the loop

Executing a query in a loop is a waste of time. It imposes unnecessary pressure on your system and may be able to get the same results faster outside of the loop. When I encounter a situation that needs to happen, I usually use two separate queries to solve the problem, and I use them to build the data array. I then iterate through the array and do not need to execute the query in this procedure.

Since WordPress applies here, it may have some exceptions. Although Get_post_meta () gets a lot of data from a database, you can use it in a loop if you're traversing the metadata for 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 and do not have a database call.

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

6. Do not use in SQL queries *

Of course, this is more like the MySQL problem, but we are used to writing SQL code in PHP, so all the same. However, if you can avoid it, do not use wildcards in SQL queries, especially when databases have many columns.

You should explicitly specify which lines are needed and simply 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 numeric 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 problems with user data: We developers do not consider every possibility, it is usually incorrect, and it may be vandalism.

Carefully considered systems can protect against these threats. Be sure to use an inline function similar to Filter_var () to check for appropriate values and to escape (or precompile) when working with the database.

WordPress has some functions to solve the problem. See articles for verification, escaping, and filtering of user data.

8. Don't pretend to be smart

Your goal should be to write elegant code to express your intentions more clearly. You might be able to optimize 0.01 seconds from each page by shortening 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 a lot of trouble.

Naming variables reasonably, writing documents for your code, prioritizing clarity rather than simplicity. 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 created longer. It is likely that no matter what you need to create, some people have already made it. Do not be afraid to seek support from others, GitHub is your good friend, composer is also, Packagist is also.

From the logging 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 retrieved by pressing the button (or clicking on the command) to use it!

10. Do not ignore other languages

If you're a PHP programmer, there's a good chance that you'll get to know at least HTML, CSS, JavaScript, and MySQL. When you're better at dealing with these languages, it's time to learn JavaScript again. JavaScript is not jQuery, you should learn JavaScript reasonably to use it more efficiently.

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, they are also 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 that you look at other frames, especially laravel.

How do you learn about Ruby, RoR, Android, IPhone, and Windows phone apps when you're using them to do a good job? You might think it's pointless because they're outside your comfort zone and the work you need, but that's what they mean. Each language has some practical things to learn, and new knowledge that has never been encountered. It's no coincidence that all the top PHP developers know a lot of other programming languages.

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.