English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Remember, in PHP 7Do Not Do in It10This Matter
1Do not use mysql_ functions
This day has finally come, and from now on, you are not just 'not supposed' to use mysql_ functions. PHP 7 has removed them all from the core, which means you need to migrate to the much better mysqli_ functions, or the more flexible PDO implementation.
2Do not write garbage code
This may be easy to understand, but it will become increasingly important because PHP 7 The speed improvement may hide some of your problems. Do not be satisfied with the speed of your site alone, because migrating to PHP 7 just make it faster.
To understand how important speed is and how to do things better, please read our beginner's guide to speed optimization.
As a developer, you should always ensure that scripts are loaded on demand, connected as much as possible, write efficient database queries, use caching as much as possible, and so on.
3Do not use PHP closing tags at the end of files
You can see that when a file ends with PHP code, most of WordPress' core code removes the ending PHP tag. In fact, the Zend framework strictly prohibits it. PHP does not need a closing tag at the end of the file, and we can ensure that no whitespace is added later by removing it.
4Do not pass references unnecessarily
I personally do not like reference passing. I know it is very practical sometimes, but in other cases, it makes the code harder to understand and more difficult to predict the results.
It is said that some people believe it makes the code run faster, but according to some senior PHP programmers, this is not true.
An example of why referencing is not good is that PHP has built-in shuffle() and sort() functions. They modify the original array instead of returning the processed array, which is very illogical.
5Do not execute queries in loops
Executing queries in a loop is very wasteful. It imposes unnecessary pressure on your system and may be able to get the same results faster outside the loop. When I encounter a situation where this is necessary, I usually use two separate queries to solve the problem, and I use them to build data arrays. Then I traverse the array without needing to execute queries in the process.
Since WordPress is applicable here, there may be some exceptions. Although get_post_meta() retrieves a large amount of data from the database, if you are iterating over a special post's metadata, you can use it in the loop. This is because when you call it for the first time, WordPress actually retrieves all metadata and caches it. Subsequent calls use these cached data without database calls.
The best way to understand these is to read the function documentation and use tools like Query Monitor.
6Do not use wildcards in SQL queries *
Of course, this is more like a MySQL problem, but we are accustomed to writing SQL code in PHP, so it's roughly the same. In any case, avoid using wildcards in SQL queries if possible, especially when the database has many columns.
You should explicitly specify which rows you need and only retrieve them. This helps reduce the resources used, protect the data, and make things as clear as possible.
For SQL, you need to understand all available functions and test their speed as much as possible. When calculating averages, sums, or similar values, use SQL functions instead of PHP functions. If you are unsure of the speed of a query, test it and try some other compilations — then use the best one.
7Do not trust user input
It is unwise to trust user input. Always validate, filter, escape, check, and leave a way out. There are three problems with user data: we developers do not consider every possibility, it is usually incorrect, and it may be deliberate sabotage.
A system carefully considered can protect against these threats. Make sure to use built-in functions like filter_var() to check appropriate values, and escape (or precompile) when handling databases.
WordPress has some functions to solve problems. See the article on validation, escaping, and filtering user data for more details.
8Do not pretend to be smart.
Your goal should be to write elegant code that clearly expresses your intentions. You may be able to optimize each page by shortening anything to a single-word variable, using multi-level ternary logic, and other means.1 This will only cause great trouble for you and the people around you.
Reasonably naming variables, documenting code writing, and prioritizing clarity over brevity. Even better, using standardized object-oriented code, which is more or less documentation on its own, without a need for a large number of inline values.
9Do not reinvent the wheel
PHP has been around for a long time, and websites have been built even longer. It's very likely that whatever you need to build, someone has already built it before. Don't be afraid to seek support from others, Github is your best friend, Composer is, and Packagist is too.
From log tools to color 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!
10Do not ignore other languages
If you are a PHP programmer, now is a good opportunity to at least understand HTML, CSS, JavaScript, and MySQL. When you can handle these languages better, it's time to re-learn JavaScript. JavaScript is not jQuery, and you should learn JavaScript reasonably to use it more efficiently.
I also plan to recommend learning object-oriented PHP, which can save time and become better when the code scale is larger. After understanding OOP, it's also easier to understand languages like C# and Java.
Expand your knowledge by understanding package managers, build scripts, CoffeeScript, LESS, SASS, YAML, script engines, and other powerful tools. I strongly recommend looking at other frameworks, especially Laravel.
How about learning Ruby, RoR, Android, iPhone, and Windows Phone application development when you use them to complete tasks brilliantly? You might think it's meaningless because they are outside of your comfort zone and the range of work you need, but that's what they mean. Each language has some practical things to learn and new knowledge you haven't encountered. All top PHP developers know a lot of other programming languages, and this is not by chance.
That's all for this article, I hope it helps with your learning, and I also hope everyone will support the Yell Tutorial more.