Blog

10 Things Not To Do In PHP 7

this article I thought I’d take a look at some of the bad patterns we should stop using as we switch to the lightning fast PHP 7

1. Do Not Use mysql_ Functions

The time has finally come when you won’t just be advised to stop using mysql_ functions. PHP 7 will remove them altogether from core which means you’ll need to move to the far better mysqli_ functions, or the even more flexible PDO implementation.

2. Do Not Write Wasteful Code

This one may be a no-brainer but it will become increasingly important because the speed increases in PHP 7 may hide some of your issues. Don’t be content with your site speed simply because the switch to PHP 7 made it faster.

To understand just how important speed is and what you can do to make things better, take a look at our Beginners’ guide to speed optimization article.

As developers you should always make sure to load scripts only when they are needed, concatenate them when possible, write efficient database queries, use caching when possible and so on.

3. Do Not Use PHP Close Tags At The End Of A file

If you take a look, most core WordPress files omit the ending PHP tag when a file ends with PHP code. In fact, the Zend Framework specifically forbids it. It is not required by PHP and by omitting it at the end of a file you are making sure that no trailing whitespace can be added.

4. Do Not Pass By Reference If Not Needed

I personally don’t like passing by reference. I understand that in some cases it is useful, but in many others it makes code harder to understand and follow and especially difficult to predict the result.

Apparently, people think it makes their code faster though which according to respectable PHP programmers is just not true.

One example of why references are bad is PHP built in shuffle() or sort(). Instead of returning a shuffled or sorted array, they modify the original which is completely illogical to my mind.

5. Do Not Perform Queries In A Loop

Performing database queries in a loop is just wasteful. It puts unnecessary strain on your systems and it is likely you can achieve the same result faster outside the loop. When I bump into a situation where this would be needed I can usually solve the issue with two separate queries I use to build an array of data. I then loop over the array, no need to perform queries in the process.

Due to the way WordPress works there may be some exceptions to this. While get_post_meta() will grab a meta value from the database, you can use it in a loop if you’re looping through one specific post’s metadata. This is because when you use it for the first time WordPress actually retrieves all metadata and caches it. Subsequent calls use the cached data, not database calls.

The best way to work these things out is to read function documentation and to use something like the Query Monitor.

6. Do Not Use * In SQL Queries

Author Details


superadmin

Leave a Reply


Your email address will not be published. Required fields are marked *

Related Posts


8th November 2018

IFÁ: Mathematics & Quantum Computing within

The Ifá Divination system, which originated in West Africa, utilizes a system of binary Mathematics to retrieve answers to life’s questions and solutions to life’s problems, from a book of knowledge called the Odús of Ifá. There are 16 major Odùs of Ifá, or “Books of Knowledge”, and within each book is contained 16 chapters,