如何检查PHP是否保留单词? [英] How can I check whether a word is reserved by PHP?

查看:75
本文介绍了如何检查PHP是否保留单词?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否存在一些用于检查单词是否已保留的功能 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?

推荐答案

http://www.php.net/manual/zh/reserved.keywords.php

您可以轻松地对其进行修改以使其适用于预定义的常量数组.

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

这可行.

<?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';

使用

$checkWord=$_POST['checkWord'];

即:

<?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>

这篇关于如何检查PHP是否保留单词?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆