In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
Today, I would like to talk to you about which 10 things not to do in PHP 7. Many people may not know much about it. In order to make you understand better, the editor has summarized the following contents for you. I hope you can get something from this article.
1. Do not use the mysql_ function
The day has finally come, and from now on you not only should not use the mysql_ function. PHP 7 has removed them all from the core, which means you need to migrate to a much better mysqli_ function, or a more flexible PDO implementation.
two。 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 makes it faster.
To understand how important speed is and how to do things better, take a look at our article's getting started guide to speed optimization.
As a developer, you should always make sure that you load scripts on demand, connect them as much as possible, write efficient database queries, use caching as much as possible, and so on.
3. Do not use PHP closing tags at the end of the file
You can see that when a file ends with PHP code, most of the WordPress core code removes the PHP tag at the end. In fact, the Zend framework specifically forbids it. PHP does not require a closed tag at the end of the file, and we can remove it to ensure that no white space characters are added later.
4. Don't pass unnecessary references.
Personally, I don't like quoting and passing. I know it's useful sometimes, but in other cases it makes the code harder to understand and harder to predict.
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 references are bad is that PHP has built-in shuffle () and sort (). It is illogical that they modify the original array instead of returning the processed array.
5. Do not execute queries in loops
It is wasteful to execute a query in a loop. It puts unnecessary pressure on your system and may be able to achieve the same results faster outside the loop. When I need this, I usually use two separate queries to solve the problem, and I use them to build data arrays. I then iterate through the array and do not need to execute the query in the process.
Because WordPress applies here, it may have some exceptions. Although get_post_meta () fetches a lot of data from the database, if you are traversing the metadata of a particular blog post, you can use it in a loop. This is because when you call it for the first time, WordPress actually takes all the metadata and caches it. Subsequent calls use these cached data and there are no database calls.
The best way to understand this is to read the function documentation and use tools like Query Monitor.
6. Do not use * in SQL queries
Of course, this is more like a MySQL problem, but we are used to writing SQL code in PHP, so it's all pretty much the same. In any case, if you can avoid it, don't use wildcards in SQL queries, especially if the database has many columns.
You should specify which rows are needed and just get them. This helps reduce the resources used, protect 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 the SQL function instead of the PHP function when calculating the mean, summation, or similar values. If you're not sure about the speed of a query, test it and try some other compilations-then use the best one.
7. Do not trust user input
It is unwise to trust user input. Always check, filter, escape, check and leave a good exit. 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 for the appropriate values and to escape (or precompile) when working with the database.
WordPress has some functions to solve the problem. For more information, see the article for 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 seconds from each page by shortening anything to an one-word variable, using multiple layers of ternary logic, and other means. But it will only cause big trouble for you and the people around you.
Name variables properly, document the code, and prefer clarity over 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 around for a long time now, and websites have been built for even longer. It's possible that whatever you need to build, some people have already built it. Don't be afraid to ask for support. Github is your best friend, so is Composer, and so is Packagist.
From logging tools to color mixing tools, from performance analyzers to unit test frameworks, from Mailchimp API to Twitter Bootstrap, everything can be obtained by pressing a key (or typing a command). Use them!
10. Don't ignore other languages.
If you are a PHP programmer, there is a good opportunity to learn at least HTML, CSS, JavaScript and MySQL. When you are better able to deal with these languages, it is time to relearn JavaScript. JavaScript is not jQuery, you should learn JavaScript reasonably to use it more efficiently.
I also intend to recommend to you to learn object-oriented PHP, which saves time and gets better when the code is larger. Languages like C # and Java are also easier to understand after you get to know OOP.
Expand your knowledge by learning about package managers, build scripts, CoffeeScript, LESS, SASS, YAML, script engines, and other powerful tools. I highly recommend that you take a look at other frameworks, especially Laravel.
When you use them to do a good job, how about learning Ruby, RoR, Android, iPhone and Windows Phone application development? You may think it's pointless because they're outside your comfort zone and what you need for work, but that's what they mean. Every 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.
After reading the above, do you have any further understanding of the 10 things not to do in PHP 7? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.