Check if Function is Available in PHP

Check if Function is Available in PHP

When working with PHP, it is crucial to verify that the functions you depend on are available before attempting to use them. This is especially important when working with functions that are not part of the PHP core and depend on specific extensions, such as GD for image processing or Curl for HTTP requests. This tutorial explains how to check if a function is available in PHP.

The function_exists is a function that helps to verify the availability of a specific function before attempting to use it. It accepts a function name as parameter and returns true if the specified function, whether built-in or user-defined, is available for use.

For example, the getimagesize function is part of the GD extension. To ensure the script runs smoothly, we can check if this function is available on the server where the PHP script is executed.

<?php

if (function_exists('getimagesize')) {
    echo 'GD extension is available';
} else {
    echo 'GD extension is not available';
}

Leave a Comment

Cancel reply

Your email address will not be published.