简体   繁体   English

如何检查定义的常量是否存在于 PHP 中?

[英]How to check if a defined constant exists in PHP?

So I'm using a PHP framework called fuelphp , and I have this page that is an HTML file, so I can't use PHP in it.所以我使用了一个名为Fuelphp的 PHP 框架,我的这个页面是一个HTML文件,所以我不能在其中使用 PHP。 I have another file that has a top bar in it, which my HTML file will call through ajax.我有另一个文件,其中有一个顶部栏,我的 HTML 文件将通过 ajax 调用它。

How do I check if a constant exists in PHP?如何检查 PHP 中是否存在常量?
I want to check for the the fuelphp framework file locations.我想检查 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...我意识到这些不是变量,它们是常量......

First, these are not variables, but constants.首先,这些不是变量,而是常量。

And you can check their existence by using the defined() function :您可以使用defined()函数检查它们的存在:

bool defined ( string $name )

Checks whether the given constant exists and is defined.检查给定的常量是否存在并已定义。

Use defined() function , for example:使用defined()函数,例如:

if (defined('VAR_NAME')) {
    // Something
}

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

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

I take it you mean CONSTANTS not variables!我认为你的意思是常量不是变量! the function is defined();函数defined();

see here: defined见这里:定义

With defined you'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 .这会起作用,但您可能会在代码编辑器(在我的情况下是带有 PHP Inteliphense 扩展的 Visual Studio Code)中收到一条烦人的错误消息,因为它找不到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:它接受一个字符串作为常量名,如果没有定义常量,则返回 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 名称作为字符串传递,因此它不会在代码编辑器上生成错误。

i use this method: 我用这个方法:

if (defined('My_variable') && (DEFAULT_LANGUAGE != '') && (DEFAULT_LANGUAGE != 'My_variable') )
{
  // your codes here
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM