Skip to content
PHP

How to be PHP 7.3 Ready When Working with Older Versions of PHP

6 min read

PHP 7.3 was released at the end of last year, but for many of us working with the latest version of PHP is not an option for a while. We are often restricted by the servers that we have to deploy to which can often be a version or two behind.

This means that for many, the new features of PHP 7.3 will remain out of reach for some time. If this describes your situation then this post is for you. We’ll look at four things you can start doing now that will help make it easier to migrate your code to PHP 7.3 when the time finally does arrive.

Here goes…

compact

The compact method allows us to create an array containing variables and their values. Personally I find this really useful for setting up view variables within a controller when working with the likes of Laravel and CakePHP. If you use compact then you want to watch out for passing undefined variables to it. As of PHP 7.3 these will be reported with a notice instead of being ignored as they have previously.

The following would be allowed before PHP 7.3:-

$foo = 'Sylvester';
compact('foo', 'bar');

The latest version of PHP will throw a notice for this code as we haven’t defined $bar but are passing it to compact. The fix is really simple, just make sure we never pass a variable to compact that we’re not defining. You can start updating your code in preparation for this now.

$foo = 'Sylvester';
compact('foo'); // We've removed 'bar' as it isn't defined

String Search Functions

Next up we’ll take a look at the string search functions: strpos, strrpos, stripos, strripos, strstr, strchr, strrchr, and stristr. These allow us to search a string ‘haystack’ for a ‘needle’ value. That means we can do things like this:-

$haystack = 'Hello World 123!';
$needle = 2;
if (strpos($haystack, $needle) !== false) {
	echo 'Needle found';
}

Prior to PHP 7.3 this would output Needle found as 2 can be found in the haystack Hello World 123! As of PHP 7.3 this will start to error because passing a non-string needle to one of the string search functions has been deprecated. The value of $needle in our example is an integer rather than a string.

We can start resolving issues like this today by ensuring that the needle used in our string search functions is always of type string.

$haystack = 'Hello World 123!';
$needle = 2;
if (strpos($haystack, (string)$needle) !== false) {
	echo 'Needle found';
}

Just remember you only need to cast the needle to a string if you know that the value isn’t a string type already, or there is some doubt over what type it will be.

Heredoc/Nowdoc

PHP has a couple of alternative syntaxes for defining strings to the more commonly used single/double quoted strings: heredoc and nowdoc. I’ve very recently written a blog post on what heredoc and nowdoc are in which I have attempted to explain the changes introduced with PHP 7.3. Basically, the latest version of the language has relaxed the rules on how the closing identifier can be used. If you’re not familiar with this syntax I’d suggest you check out my blog post.

What we need to watch out for in our existing code is that we don’t duplicate the closing identifier somewhere within a heredoc/nowdoc string. Doing so will cause issues when we finally update to PHP 7.3.

<<<FOO
abcdefg
	FOO
FOO;

The above code is valid in versions of PHP before 7.3, but due to the changes being introduced it will start causing syntax errors. So we want to keep an eye open for any heredoc/nowdoc blocks in our code and ensure we’re not repeating the identifier in the string. So for this example FOO is the indentifier and is repeated on the second line of the string; to fix we just want to change the identifier to something unique. For example:-

<<<BAR
abcdefg
	FOO
BAR;

While not taking full benefit of the relaxed rules this is now at least valid syntax in all recent versions of PHP.

Case-insensitive Constants

Finally, case-insensitive constants are no longer going to be permitted in PHP 7.3, so now is the time to start weeding them out. I suspect this one won’t affect many people as I don’t ever recall seeing this actually being done.

Prior to PHP 7.3 you could write code like this:-

define('GREETING', 'Hello you', true);
echo GREETING;
echo greeting;

That third parameter for the define method is saying that we can use the defined constant using mixed cases; therefore the two echo statements that follow the definition of the constant would both be valid. This has been deprecated as of PHP 7.3. So now is the time to drop using that third parameter in define and ensuring we consistently use the same case for the constants wherever we call them.

define('GREETING', 'Hello you');
echo GREETING;
// We've got rid of echo greeting; as this will no longer be valid

Thanks for Reading

I hope that you’ve found this interesting. There’s always a buzz of excitement about new PHP releases, but so often the reality is many of us can’t take advantage of the changes for at least a few months if not years. If this has been useful please consider sharing this article.

You might also be interested in my What are PHP Heredoc & Nowdoc? article I mentioned.

Lastly, you can follow me on Twitter for updates on further posts on my blog.

© 2024 Andy Carter