PHP Version Compatibility
PHP is in an ongoing process of development, and there are multiple
versions. If you need to check whether a particular function is available
to your code, you can use the function_exists function, which checks all
predefined and user-created functions.
Example 5-9 checks for
the function array_combine, which is
specific to PHP version 5.
<?php
if (function_exists("array_combine"))
{
echo "Function exists";
}
else
{
echo "Function does not exist - better write our own";
}
?>Using code such as this, you can identify any features available in newer versions of PHP that you will need to replicate if you want your code to still run on earlier versions. Your functions may be slower than the built-in ones, but at least your code will be much more portable.
You can also use the phpversion
function to determine which version of PHP your code is running on. The
returned result will be similar to the following, depending on the
version:
5.2.8
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access