10 Things Not To Do In PHP 7
1. Do Not Use mysql_ Capabilities
The time has lastly come once you received’t simply be suggested to cease utilizing
mysql_ capabilities. PHP 7 will take away them altogether from core which suggests you’ll want to maneuver to the much better
mysqli_ capabilities, or the much more versatile PDO implementation.
2. Do Not Write Wasteful Code
This one could also be a no brainer however it would turn out to be more and more essential as a result of the pace will increase in PHP 7 might conceal a few of your points. Don’t be content material together with your website pace just because the change to PHP 7 made it sooner.
To grasp simply how essential pace is and what you are able to do to make issues higher, check out our Beginners’ guide to speed optimization article.
As builders you need to all the time be sure that to load scripts solely when they’re wanted, concatenate them when attainable, write environment friendly database queries, use caching when attainable and so forth.
3. Do Not Use PHP Shut Tags At The Finish Of A file
Should you have a look, most core WordPress information omit the ending PHP tag when a file ends with PHP code. The truth is, the Zend Framework particularly forbids it. It’s not required by PHP and by omitting it on the finish of a file you make positive that no trailing whitespace may be added.
4. Do Not Move By Reference If Not Wanted
I personally don’t like passing by reference. I perceive that in some instances it’s helpful, however in lots of others it makes code tougher to grasp and observe and particularly tough to foretell the consequence.
Apparently, folks assume it makes their code sooner although which based on respectable PHP programmers is simply not true.
One instance of why references are unhealthy is PHP in-built
kind(). As a substitute of returning a shuffled or sorted array, they modify the unique which is totally illogical to my thoughts.
5. Do Not Carry out Queries In A Loop
Performing database queries in a loop is simply wasteful. It places pointless pressure in your methods and it’s probably you’ll be able to obtain the identical consequence sooner exterior the loop. Once I bump right into a scenario the place this may be wanted I can normally clear up the difficulty with two separate queries I take advantage of to construct an array of information. I then loop over the array, no have to carry out queries within the course of.
As a result of method WordPress works there could also be some exceptions to this. Whereas
get_post_meta() will seize a meta worth from the database, you need to use it in a loop when you’re looping by means of one particular publish’s metadata. It is because once you use it for the primary time WordPress truly retrieves all metadata and caches it. Subsequent calls use the cached information, not database calls.
One of the simplest ways to work these items out is to learn perform documentation and to make use of one thing just like the Query Monitor.