如何检查一个常量是否存在于 PHP 中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14429321/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to check if a constant exists in PHP
提问by ryanc1256
So I'm using a PHP framework called fuelphp, and I have this page that is an HTMLfile, so I can't use PHP in it. I have another file that has a top bar in it, which my HTML file will call through ajax.
所以我使用了一个名为Fuelphp的 PHP 框架,我的这个页面是一个HTML文件,所以我不能在其中使用 PHP。我有另一个文件,其中有一个顶部栏,我的 HTML 文件将通过 ajax 调用它。
How do I check if a constant exists in PHP?
I want to check for the the fuelphp framework file locations.
如何检查 PHP 中是否存在常量?
我想检查 Fuelphp 框架文件的位置。
These are the constants I need to check for (actually, I only have to check one of them):
这些是我需要检查的常量(实际上,我只需要检查其中一个):
define('DOCROOT', __DIR__.DIRECTORY_SEPARATOR);
define('APPPATH', realpath(__DIR__.'/fuel/app/').DIRECTORY_SEPARATOR);
define('PKGPATH', realpath(__DIR__.'/fuel/packages/').DIRECTORY_SEPARATOR);
define('COREPATH', realpath(__DIR__.'/fuel/core/').DIRECTORY_SEPARATOR);
require APPPATH.'bootstrap.php';
edit:
I realized that these aren't variables they are constants...
编辑:
我意识到这些不是变量,它们是常量......
回答by Tom Walters
回答by Eric MORAND
回答by Niklas
Check using defined('CONSTANT')function.
检查使用defined('CONSTANT')功能。
An example from the manual:
手册中的一个例子:
<?php /* Note the use of quotes, this is important. This example is checking * if the string 'TEST' is the name of a constant named TEST */ if (defined('TEST')) { echo TEST; } ?>
<?php /* Note the use of quotes, this is important. This example is checking * if the string 'TEST' is the name of a constant named TEST */ if (defined('TEST')) { echo TEST; } ?>
回答by Svetoslav Marinov
here's a cooler & more concise way to do it:
这是一个更酷更简洁的方法:
defined('CONSTANT') or define('CONSTANT', 'SomeDefaultValue');
credit: daniel at neville dot tk https://www.php.net/manual/en/function.defined.php#84439
信用:丹尼尔在 neville dot tk https://www.php.net/manual/en/function.defined.php#84439
回答by Zac
回答by T.Todua
i use this method:
我用这个方法:
if (defined('My_variable') && (DEFAULT_LANGUAGE != '') && (DEFAULT_LANGUAGE != 'My_variable') )
{
// your codes here
}
回答by Marlon
With definedyou'll have to do something like that:
随着defined你必须做这样的事情:
if (defined("CONST_NAME"))
$value = CONST_NAME;
This will work, but you'll could get an annoying error message in your code editor (in my case Visual Studio Code with PHP Inteliphense extension) for the second line, since it wont find CONST_NAME.
Another alternative would be to use the constant function. It takes an string as the constant name and returns null if the constant is not defined:
这会起作用,但您可能会在代码编辑器(在我的情况下为带有 PHP Inteliphense 扩展名的 Visual Studio Code)中收到一条烦人的错误消息,因为它找不到CONST_NAME. 另一种选择是使用常量函数。它接受一个字符串作为常量名,如果没有定义常量,则返回 null:
$value = constant("CONST_NAME");
if ($value != null)
{
// Use the value ...
}
Since you passed the const name as a string, it wont generate an error on the code editor.
由于您将 const 名称作为字符串传递,因此它不会在代码编辑器上生成错误。

