How can I check whether a word is reserved by PHP?

Is there some function for checking whether a word is reserved in PHP or I can use it myself? I can check it manually: just use it and see the error or warning, but I need to automate this check. Is there any way to do this?


Solution 1:

you can build your own automated function. To do that use an array that hold all the reserved word.Check out for more information

Solution 2:

Array borrowed from http://www.php.net/manual/en/reserved.keywords.php

You could easily modify it to work for the predefined constants array.

This works.

<?php
$keywords = array('__halt_compiler', 'abstract', 'and', 'array', 'as', 'break', 'callable', 'case', 'catch', 'class', 'clone', 'const', 'continue', 'declare', 'default', 'die', 'do', 'echo', 'else', 'elseif', 'empty', 'enddeclare', 'endfor', 'endforeach', 'endif', 'endswitch', 'endwhile', 'eval', 'exit', 'extends', 'final', 'for', 'foreach', 'function', 'global', 'goto', 'if', 'implements', 'include', 'include_once', 'instanceof', 'insteadof', 'interface', 'isset', 'list', 'namespace', 'new', 'or', 'print', 'private', 'protected', 'public', 'require', 'require_once', 'return', 'static', 'switch', 'throw', 'trait', 'try', 'unset', 'use', 'var', 'while', 'xor');

$predefined_constants = array('__CLASS__', '__DIR__', '__FILE__', '__FUNCTION__', '__LINE__', '__METHOD__', '__NAMESPACE__', '__TRAIT__');

$checkWord='break'; // <- the word to check for.
if (in_array($checkWord, $keywords)) {
    echo "Found.";
}

else {
echo "Not found.";
}

?>

You could also implement this in conjunction with a form by replacing:

$checkWord='break';

with

$checkWord=$_POST['checkWord'];

I.e.:

<?php

if(isset($_POST['submit'])){
$keywords = array('__halt_compiler', 'abstract', 'and', 'array', 'as', 'break', 'callable', 'case', 'catch', 'class', 'clone', 'const', 'continue', 'declare', 'default', 'die', 'do', 'echo', 'else', 'elseif', 'empty', 'enddeclare', 'endfor', 'endforeach', 'endif', 'endswitch', 'endwhile', 'eval', 'exit', 'extends', 'final', 'for', 'foreach', 'function', 'global', 'goto', 'if', 'implements', 'include', 'include_once', 'instanceof', 'insteadof', 'interface', 'isset', 'list', 'namespace', 'new', 'or', 'print', 'private', 'protected', 'public', 'require', 'require_once', 'return', 'static', 'switch', 'throw', 'trait', 'try', 'unset', 'use', 'var', 'while', 'xor');

$predefined_constants = array('__CLASS__', '__DIR__', '__FILE__', '__FUNCTION__', '__LINE__', '__METHOD__', '__NAMESPACE__', '__TRAIT__');
$checkWord=$_POST['checkWord'];

if (in_array($checkWord, $keywords)) {
    echo "FOUND!!";
}

else {
echo "Not found.";
   }

}

?>

<form method="post" action="">

Enter word to check: 
<input type="text" name="checkWord">

<br>
<input type="submit" name="submit" value="Check for reserved word">
</form>

A different version using both arrays set inside a form.

It could stand for some polishing up, but it does the trick

<?php

if(isset($_POST['submit'])){
$keywords = array('__halt_compiler', 'abstract', 'and', 'array', 'as', 'break', 'callable', 'case', 'catch', 'class', 'clone', 'const', 'continue', 'declare', 'default', 'die', 'do', 'echo', 'else', 'elseif', 'empty', 'enddeclare', 'endfor', 'endforeach', 'endif', 'endswitch', 'endwhile', 'eval', 'exit', 'extends', 'final', 'for', 'foreach', 'function', 'global', 'goto', 'if', 'implements', 'include', 'include_once', 'instanceof', 'insteadof', 'interface', 'isset', 'list', 'namespace', 'new', 'or', 'print', 'private', 'protected', 'public', 'require', 'require_once', 'return', 'static', 'switch', 'throw', 'trait', 'try', 'unset', 'use', 'var', 'while', 'xor');

$predefined_constants = array('__CLASS__', '__DIR__', '__FILE__', '__FUNCTION__', '__LINE__', '__METHOD__', '__NAMESPACE__', '__TRAIT__');

$checkWord=$_POST['checkWord'];

$checkconstant=$_POST['checkconstant'];

if (in_array($checkWord, $keywords)) {
    echo "<b>Reserved word FOUND!!</b>";
    echo "\n";
}

else {
echo "Reserved word not found or none entered.";
   }

if (in_array($checkconstant, $predefined_constants)) {
    echo "<b>Constant word FOUND!!</b>";
    echo "\n";
}

else {
echo "Constant not found or none entered.";
   }

}

?>

<form method="post" action="">

Enter reserved word to check: 
<input type="text" name="checkWord">

Enter constant word to check: 
<input type="text" name="checkconstant">

<br><br>
<input type="submit" name="submit" value="Check for reserved words">
<input type="reset" value="Reset" name="reset">
</form>

Solution 3:

Yes, you can. As you mentioned in link, we have a list of this names, so why just check in this way?:

   $keywords = array('__halt_compiler', 'abstract', 'and', 'array', 'as', 'break', 'callable', 'case', 'catch', 'class', 'clone', 'const', 'continue', 'declare', 'default', 'die', 'do', 'echo', 'else', 'elseif', 'empty', 'enddeclare', 'endfor', 'endforeach', 'endif', 'endswitch', 'endwhile', 'eval', 'exit', 'extends', 'final', 'for', 'foreach', 'function', 'global', 'goto', 'if', 'implements', 'include', 'include_once', 'instanceof', 'insteadof', 'interface', 'isset', 'list', 'namespace', 'new', 'or', 'print', 'private', 'protected', 'public', 'require', 'require_once', 'return', 'static', 'switch', 'throw', 'trait', 'try', 'unset', 'use', 'var', 'while', 'xor');

 var_dump(in_array('__halt_compiler',$keywords)); 

Solution 4:

I wrote this function isRW(string $string) NULL|false|string try it.. it will return NULL if the input is not a word, and will return a boolean false if the input is not reserved, and a string (PHP lexical token name) if the input is reserved..

function isRW($str){
    if(!preg_match("/^\w{1,20}/", $str)) return null; $rsv=false;
    $uc  = ['true','false','parent','self']; //uncheckable list
    $tk  = token_get_all('<?php '.(boolval($str)? $str:'x')); $tknm=token_name($tk[1][0]); 
    $cst = get_defined_constants(true); unset($cst['user']); global $phpcst; 
    $phpcst = !isset($phpcst)? array_merge(...array_column($cst, null)): $phpcst;

    if($tknm!='T_STRING'
    or in_array($str, $uc)  //if it's uncheckable 
    or @settype($str, $str) //if it's a type like int,NULL,object..
    or preg_match("/__\w/", $str) //PHP reserved all methods prefixed by "__" for future use
    or array_key_exists($str, $phpcst) //if it's a PHP const
    or (function_exists($str) and (new \ReflectionFunction($str))->isInternal())
    or (class_exists($str)    and (new \ReflectionClass($str))->isInternal())) $rsv=true;

    return $rsv? $tknm: $rsv;
}

some test outputs :

isRW('}');                  //return null
isRW('foobar');             //return boolean false
isRW('void');               //return boolean false
isRW('isInternal');         //return boolean false
isRW('_call');              //return boolean false
isRW('__call');             //return string T_STRING
isRW('__halt_compiler');    //return string T_HALT_COMPILER
isRW('private');            //return string T_PRIVATE
isRW('__DIR__');            //return string T_DIR
isRW('global');             //return string T_GLOBAL
isRW('E_ERROR');            //return string T_STRING
isRW('DATE_ISO8601');       //return string T_STRING
isRW('PHP_SAPI');           //return string T_STRING
isRW('namespace');          //return string T_NAMESPACE
isRW('function');           //return string T_FUNCTION
isRW('ReflectionClass');    //return string T_STRING
isRW('abstract');           //return string T_ABSTRACT
isRW('self');               //return string T_STRING
isRW('array');              //return string T_ARRAY
isRW('is_array');           //return string T_STRING
isRW('callable');           //return string T_CALLABLE
isRW('isset');              //return string T_ISSET
isRW('and');                //return string T_LOGICAL_AND
isRW('echo');               //return string T_ECHO