So im using a php framework called fuelphp, and I have this page that is a html file so i can't use php in it, and I have another file that has a top bar in it, which my html file will call though ajax. And I was just asking, how do we check if a constant exists in php? aka (the fuelphp framework file locations)
我使用一个叫fuelphp的php框架,这个页面是一个html文件,所以我不能在里面使用php,我还有一个文件里面有一个顶栏,我的html文件用ajax调用。我只是问,如何检查php中是否存在常数?aka (fuelphp框架文件位置)
so these are constants I need to check (well 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 realised that these aren't variables they are constants...
编辑:我意识到这些不是变量它们是常量……
6 个解决方案
#1
45
First, these are not variables, but constants.
首先,这些不是变量,而是常量。
And you can check their existence by using the defined()
function :
通过定义()函数可以检验它们的存在性:
bool defined ( string $name )
Checks whether the given constant exists and is defined.
检查给定的常数是否存在和是否定义。
#2
#3
18
Check using defined('CONSTANT')
function.
检查使用(“常数”)函数定义。
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; } ?>
#4
7
here's a cooler & more concise way to do it:
这里有一个更酷更简洁的方法:
defined('CONSTANT') or define('CONSTANT', 'SomeDefaultValue');
credit: daniel at neville dot tk http://php.net/manual/bg/function.defined.php
感谢:daniel在neville dot tk http://php.net/manual/bg/function.definedphp
#5
5
I take it you mean CONSTANTS not variables! the function is defined();
我猜你是说常数而不是变量!定义的函数();
see here: defined
在这里看到的:定义
#6
0
i use this method:
我用这个方法:
if (defined('My_variable') && (DEFAULT_LANGUAGE != '') && (DEFAULT_LANGUAGE != 'My_variable') )
{
// your codes here
}
#1
45
First, these are not variables, but constants.
首先,这些不是变量,而是常量。
And you can check their existence by using the defined()
function :
通过定义()函数可以检验它们的存在性:
bool defined ( string $name )
Checks whether the given constant exists and is defined.
检查给定的常数是否存在和是否定义。
#2
#3
18
Check using defined('CONSTANT')
function.
检查使用(“常数”)函数定义。
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; } ?>
#4
7
here's a cooler & more concise way to do it:
这里有一个更酷更简洁的方法:
defined('CONSTANT') or define('CONSTANT', 'SomeDefaultValue');
credit: daniel at neville dot tk http://php.net/manual/bg/function.defined.php
感谢:daniel在neville dot tk http://php.net/manual/bg/function.definedphp
#5
5
I take it you mean CONSTANTS not variables! the function is defined();
我猜你是说常数而不是变量!定义的函数();
see here: defined
在这里看到的:定义
#6
0
i use this method:
我用这个方法:
if (defined('My_variable') && (DEFAULT_LANGUAGE != '') && (DEFAULT_LANGUAGE != 'My_variable') )
{
// your codes here
}