Php Chinese network (www.php.cn) provides the most comprehensive basic tutorial on programming technology, introducing HTML, CSS, Javascript, Python, Java, Ruby, C, PHP, basic knowledge of MySQL and other programming languages. At the same time, this site also provides a large number of online instances, through which you can better learn programming...
1. do not use the mysql _ function
This day has finally arrived. Since then, you have not just "shouldn't" use the mysql _ function. PHP 7 has removed all of them from the core, that is, you need to migrate to a much better mysqli _ function, or more flexible PDO implementation.
2. do not write junk code
This one may be easy to understand, but will become more and more important, because the speed improvement of PHP 7 may hide some of your problems. Not just for the speed of your site, because migrating to PHP 7 makes it faster.
To understand how important speed is and how to make things better, take a look at our article speed optimization Getting Started Guide.
As a developer, you should always ensure that you load scripts on demand, connect to them as much as possible, write efficient database queries, use cache as much as possible, and other.
3. do not use PHP close labels at the end of the file
You can see that when a file ends with a PHP code, most of the WordPress core code removes the PHP tag at the end. In fact, the Zend Framework is particularly forbidden. PHP does not need to close the tag at the end of the file, and we can remove it to ensure that no blank characters will be added to the end.
4. do not transmit unnecessary references.
I personally do not like to pass references. I know that sometimes it is useful, but in other cases it makes the code hard to understand and it is more difficult to predict the results.
It is said that some people think it makes code run faster, but according to some advanced PHP programmers, this is not correct.
An example of why the reference is poor is that PHP has built-in shuffle () and sort (). They modify the original array instead of returning the processed array, which is not logical.
5. do not execute queries in a loop
Query execution in a loop is a waste. It puts unnecessary pressure on your system and may be able to get the same result faster outside the loop. When I need such a situation, I usually use two separate queries to solve the problem, and I will use them to build data arrays. Then I will traverse the array and do not need to execute the query in this process.
WordPress may have some exceptions because it applies here. Although get_post_meta () will obtain a large amount of data from the database, if you are traversing the metadata of a special blog, you can use it in a loop. This is because when you call it for the first time, WordPress will actually get all the metadata and cache them. Subsequent calls use the cached data without database calls.
The best way to understand this is to read the function documentation and use tools similar to Query Monitor.
6. do not use it in SQL queries *
Of course, this is more like a MySQL problem, but we are used to writing SQL code in PHP, so it is almost the same. In any case, do not use wildcards in SQL queries, especially when there are many columns in the database.
You should specify the required rows and obtain them only. This helps reduce the resources used, protect 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 the mean, sum, or a similar value, you must use an SQL function instead of a PHP function. If you are not sure about the speed of a query, test it and try other compilation methods to use the best one.
7. do not trust user input
It is unwise to trust user input. Always check, filter, escape, and check and leave a retreat. There are three problems with user data: We developers do not consider every possibility, it is usually incorrect, and it may be intentionally damaged.
Well-considered systems can defend against these threats. Make sure that you use built-in functions like filter_var () to check the appropriate values and Escape (or pre-compile) when processing the database ).
WordPress has some functions to solve the problem. For more information, see Article verification, conversion, and filtering of user data.
8. do not pretend to be smart.
Your goal should be to write elegant code to clearly express your intention. You may be able to reduce anything to a variable of a word, use multi-layer ternary logic, and other means to optimize 0.01 seconds per page. But this will only cause a lot of trouble for you and the people around you.
Name variables properly and write documents for the code. give priority to clarity rather than simplicity. It is even better to use standard object-oriented code, which is more or less a document and does not require a large number of inline values.
9. do not re-invent the wheel
It has been a long time since PHP was created, and the website has been created for a longer time. It is very likely that no matter what you need to create, some people have already created it. Don't be afraid to seek support from others. Github is your good friend, so is Composer, and Packagist.
From the log tool to the color tool, from the performance analyzer to the unit test framework, from the Mailchimp API to Twitter Bootstrap, everything can be obtained by pressing the key (or clicking the command, use them!
10. do not ignore other languages
If you are a PHP programmer, you have a good opportunity to learn at least about HTML, CSS, JavaScript, and MySQL. When you can better process these languages, it is time to re-learn JavaScript. JavaScript is not jQuery. you should learn JavaScript reasonably to use it more efficiently.
I also plan to recommend you to learn object-oriented PHP, which can save time and become better when the code is larger. For languages similar to C # and Java, after you understand OOP, they are also easier to understand.
Expand your knowledge by understanding the package manager, build scripts, CoffeeScript, LESS, SASS, YAML, script engine, and other powerful tools. I strongly recommend you to look at other frameworks, especially Laravel.
How do you learn Ruby, RoR, Android, iPhone, and Windows Phone application development when you use them to complete tasks? You may think this is meaningless because they are out of your comfort zone and work needs, but that is what they mean. Each language has some practical things to learn and new knowledge that has never been met. It is no coincidence that all top PHP developers know many other programming languages.