10 pits you need to avoid in PHP 7

Source: Internet
Author: User
Tags closing tag zend framework ruby on rails
This article to share the content is 10 you need in PHP 7 to avoid the pit, has a certain reference value, the need for friends can refer to

1. Do not use the Mysql_ class function

Finally, you don't have to see the suggestion to use mysql_ the function again. Since PHP 7 completely removes them from the core, this means you move to a better mysqli_ class function, or a more flexible PDO layer.

2. Don't write useless code

This may seem like a no-brain suggestion, but as the PHP7 speed increases, it obscures some of the problems that make it increasingly important. Don't just cut the PHP7 to make the site faster, and you'll be complacent.

To understand the importance of speed and how to do it better, take a look at our beginner's Accelerated Optimization guide for this article.

As a developer, you should make sure that you load scripts on demand, combine them when possible, write efficient database query statements, use caching if possible, and so on.

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

If you look around, you'll find that most of the WordPress core code files end up omitting the PHP closing tag. In fact, the Zend framework specifically prohibits the closure of tags. It is not necessary for PHP to omit it at the end of the file to ensure no extra whitespace at the end.

4. If you do not have to refer to the parameters

I personally do not like to refer to the argument. I certainly know that it is useful in some situations, but in most cases it makes the code difficult to understand, difficult to follow, and unpredictable.

It is thought that references can make their code faster, but as the respectable PHP Programmer's article points out, this is not the case.

PHP's built-in shuffle() or sort() functions are bad references to reference cases. It modifies the original array instead of returning an scrambled or sequenced array, which is completely against our will.

5. Do not use queries in loops

The worst thing to do when using a database query in a loop. He will put undue pressure on the system, and it is possible that you can get the same results faster by using queries outside of the loop. When I come across a scene that has to be used this way, I usually resolve it by dividing it into two queries to construct an array. Then loop through the array without having to loop the query.

Due to the way WordPress works, there may be some exceptions to this. You get_post_meta() will get a metadata from the database that you can use in a loop if you are iterating through the metadata for a particular article. This is because when you use it for the first time, WordPress actually gets all the metadata and caches it. The subsequent call is actually invoking the cached data instead of calling the database.

The best way to solve these problems is to read the function documentation and use something similar to the query listener.

6. Do not use * in SQL queries

Well, it's more like a MySQL problem, but we're more inclined to write SQL statements in code, so I'm saying it's a fair game. In any case, if you can avoid using wildcards, don't use them, especially if your database has a lot of fields.

Explicitly specify the fields you want, and retrieve only those fields. This helps save memory, protect data, and make things clearer.

In terms of SQL, learn as much as possible about your available functions and test the speed. When calculating averages, summing, and calculating similar numbers, use SQL built-in functions instead of PHP functions. If you're not sure how fast a query is, test it and compare it with other practices to pick the best one.

7. Do not trust the user's input

Trusting user input is unwise. For user input, always need to filter, disinfect, escape, verify and use fallback. There are three issues with user input: It is impossible for us developers to take into account all possibilities, often errors, intentional malicious input.

A well-thought-out system can prevent all of these problems. When using a database, be sure to use built-in functions, such as filter_var() , to check for legitimacy, to escape, and other things to do.

WordPress has a bunch of functions that can help you. Take a look at this article to learn more about Validating, escaping and sanitising user data.

8. Don't be smart

Your goal is to write an elegant code that clearly expresses your wishes. You can save 0.01 seconds of load time for each page by shortening the variable name, using multi-level trinocular logic, and other cleverness, but it's not worth the cost of being able to grow your and your team's headaches.

Appropriately named variables to write code documents in a concise and straightforward manner. A better approach is to use standardized object-oriented code styles to write documents more or less, rather than using a large number of inline code annotations.

9. Do not create wheels repeatedly

PHP has been around for some time, and web site development has been even longer. No matter what you have done, the predecessors must have done it. Don't be afraid to rely on the support of others, Github, Composer, packagist are your mentor.

From the log to the color processor, from the parser to the unit test framework, from Mailchimp APIs to Twitter Bootstrap, all you have to do is press a button (or hit a command) to use it!

10. Don't neglect other languages

If you are a phper, the standard practice now is to know at least Html,css,javascript and MySQL. When you are able to deal with these languages well, it is time to learn Javascript again. Javascript is not jQuery. You should learn Javascript to make effective use of jQuery.

I also recommend learning everything PHP object-oriented. It's a lifesaver that allows your code to be upgraded in order of magnitude. It can also open doors like the C # and Java languages, and after you have these experiences, they make it easier to understand object-oriented programming (OOP).

Expand your knowledge by learning package management, building scripts, Coffeescript, less, SASS, YAML, template engines, and other useful tools. I also heartily recommend to look at other frameworks, especially Laravel.

Consider Ruby, Ruby on Rails and android,iphone,windows Phone app development When you've done enough? You may think that this is meaningless because they are not within your comfort zone and your job needs, but they are exactly the point. Each language has some useful teaching knowledge and some harmless extra knowledge. It's no coincidence that all the top PHP developers know about other programming languages!

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: info-contact@alibabacloud.com 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.