Home > News content

10 things not to do in PHP 7

via:博客园     time:2016/9/18 9:51:04     readed:1129

1 do not useMysql_function

This day has finally come, from now on you are not onlyMysql_Function.PHP 7 has been removed from the core, that is, you need to migrate to a much betterMysqli_Function, or 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 of PHP 7 upgrade may hide some of your problems. Do not just meet the speed of your site, because migrating to PHP 7 to make it faster.

In order to understand how important the speed is, and how to do things better, please take a look at our article.Introduction to speed optimization guide.

As a developer, you should always make sure that you need to load scripts, connect them as much as possible, write efficient database queries, and use the cache as much as possible.

3 do not use the PHP closing tag at the end of the file

You can take a look at the end of a file with PHP code, WordPress most of the core code to the end of the PHP tag was removed. In fact, the Zend framework in particularForbid it.PHP does not require a closing tag at the end of the file, and we can guarantee that it will not add any blank characters in the back.

4 don't make unnecessary references.

I personally don't like to quote. I know sometimes it's very useful, but in other cases it makes the code more difficult to understand, and more difficult to predict.

It is said that some people think that it makes the code run faster, but according to theSome PHP senior programmersThat is not true.

Explains why a good example is not a good example of PHPShuffle ()andSort ()They modify the original array, instead of returning an array, which is not logical.

5 do not execute queries in the loop.

Execute the query in the circulation. It is a waste of your system under undue pressure, and may be able to obtain the same results in the external circulation faster. When I encountered such a situation, I usually use two separate queries to solve the problem, I will use them to build the data array. I will traverse the array, does not need to execute the query in the process.

Since WordPress applies here, it may have some exceptions.Get_post_meta ()Will get a lot of data from the database, if you are a special post traversal metadata, you can use it in a loop. This is because when you first call it, WordPress will actually get all the metadata cache and cache data using them. These subsequent calls, no database calls.

The best way to understand the function of reading is the document, and the use of similarMonitor QueryTools.

6 do not use in the SQL query

Of course, this is more like MySQL, but we used to write SQL code in PHP, so almost all. In any case, if you can avoid it, do not use wildcards in SQL query, especially when there are many database columns.

You should explicitly specify what you need to do, and just get them. This helps to reduce the resources, protect the data, and make things as clear as possible.

For SQL, you need to understand all the functions available, and to test its speed as much as possible. In the calculation of the mean, sum or similar numerical calculation, in order to use the SQL function instead of PHP. If you do not specify a query speed, test it and try some other compiler -- the use of the best.

7 do not trust user input

Trust user input is not sensible. Always check, filtering, escape, check and leave a good escape. Three problems of user data: we developers did not consider every possibility, it usually is not correct, and it could sabotage.

After careful consideration of the system can protect these threats. To ensure that the use of similarFilter_var ()The built-in function to check the appropriate value, as well as in the database when the escape (or pre compiled).

WordPress has a number of functions to solve the problem.Check, escape and filtering of user data.

8 don't be clever

Your goal should be to write elegant code, to more clearly express your intention. You may be able to turn anything into a shorter word variable, three yuan logic layers, and other means, from each page optimization for 0.01 seconds. But this will only have a big trouble for you and you the people around you.

Reasonable naming variables, writing documents for the code, the priority is clear and not simple. Even better, the use of standard object-oriented code, it is more or less the documentation, do not need a lot of inline value.

9 don't reinvent the wheel.

PHP up to now, for a long time, the site has been built for a long time. It is likely that whatever you need to create, some people have long ago made it. Don't be afraid to ask for support from others,GithubIs your good friend,Also,Packagist.

From the log tool to the palette tool from the performance analyzer to unit testing framework, from Mailchimp API to Twitter Bootstrap, everything can press the button (or on command) to obtain and use them!

10 don't ignore other languages

If you are a PHP programmer, now has a good chance to understand at least HTML, CSS, JavaScript and MySQL. when you can better deal with these languages, is to learn JavaScript time.JavaScript is not jQuery, you should reasonably learn JavaScript to more effectively use it.

I also intend to recommend learning object oriented PHP to you, it can save time, and will get better in larger code. For the similar C# and Java language, understand OOP in you, they are easier to understand.

By understanding the package manager, build scripts, CoffeeScript, LESS, SASS, YAML, scripting engines, and other powerful tools to extend your knowledge, I strongly recommend to you a look at other frameworks, especiallyLaravel.

When you use them to complete the task, learning Ruby, RoR, Android, iPhone and Windows to Phone application development? You might think this is meaningless, because the number of them in your comfort zone and work scope, but this is their meaning. Each language has to learn some practical things, and new knowledge never met. All the top PHP developers know many other programming languages, this is not accidental.

China IT News APP

Download China IT News APP

Please rate this news

The average score will be displayed after you score.

Post comment

Do not see clearly? Click for a new code.

User comments