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 take advantage of features in newer versions of PHP and yet still have your code run on earlier versions, as long as you replicate any features that are missing. 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
version:
5.2.8
Get Learning PHP, MySQL, and JavaScript now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.