Check a given string corresponds to a built-in PHP function or predefined special variable?-ThrowExceptions

Exception or error:

I’m able to check whether a string corresponds to a “reserved keyword“, such as “if”, “for” and “else”, with this code (given by a Stack Exchange user in a previous question):

function is_reserved_php_keyword($string)
{
    $tokens = token_get_all('<?php ' . $string . '; ?>');
    return reset($tokens[1]) !== T_STRING;
}

Although a bit “hacky”, it seems to work flawlessly for PHP keywords.

However, I wish to also be able to check if a string corresponds to:

  1. A built-in (not just “any existing”) PHP function, including “mb_strpos” and “dl”.
  2. A “special global variable” such as “$argc”, “$argv”, “$_GET”, “$_POST”, “$php_errormsg”, etc.

I want to avoid creating a hardcoded list of these in my code, as that would require me to perpetually and manually keep this up to date with all future PHP versions, even if I manage to figure out all the currently existing ones.

I’ve looked long and hard in both the manual and online for something like this without any luck. Maybe somebody knows of a “trick” similar to the above is_reserved_php_keyword() function?

The similar earlier questions sadly have no acceptable answers.

How to solve:

I miraculously, unexpectedly just figured out half of my question on my own:

function is_built_in_php_function($function_name)
{
    if (in_array($function_name, get_defined_functions()['internal']))
        return true;

    return false;
}

Leave a Reply

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