Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. I have another file that has a top bar in it, which my HTML file will call through ajax.

How do I check if a constant exists in PHP?
I want to check for the the fuelphp framework file locations.

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

like image 785
ryanc1256 Avatar asked Jan 20 '13 21:01

ryanc1256


People also ask

How do you know if a constant exists?

To check if constant is defined use the defined function. Note that this function doesn't care about constant's value, it only cares if the constant exists or not. Even if the value of the constant is null or false the function will still return true .

How check is defined in PHP?

PHP isset() Function The isset() function checks whether a variable is set, which means that it has to be declared and is not NULL. This function returns true if the variable exists and is not NULL, otherwise it returns false.

How is constant defined in a PHP script?

A constant is an identifier (name) for a simple value. The value cannot be changed during the script. A valid constant name starts with a letter or underscore (no $ sign before the constant name). Note: Unlike variables, constants are automatically global across the entire script.

What is define () in PHP?

Definition and Usage The define() function defines a constant. Constants are much like variables, except for the following differences: A constant's value cannot be changed after it is set. Constant names do not need a leading dollar sign ($) Constants can be accessed regardless of scope.

How to see if a constant has been defined with PHP?

Checking to see if a constant has been defined with PHP Checking to see if a function has already been defined in PHP is done using the defined () function which works in a similar way to the define () function. A single parameter is passed in which again needs to be a single or double quoted string.

How to check if a constant exists in a given variable?

And you can check their existence by using the defined () function : Checks whether the given constant exists and is defined. And to save a few extra minutes of debugging, make sure the VAR_NAME is included in quotes, as shown in this answer and in the docs. Adding the quotes, so simple but so simple to overlook!

How to check if a variable or function exists in Python?

If you want to see if a variable exists, use isset () as defined () only applies to constants. If you want to see if a function exists, use function_exists () . The constant name. Returns true if the named constant given by constant_name has been defined, false otherwise. /* Note the use of quotes, this is important. This example is checking

Why can't I See my constants in all defined?

All defined is doing is verifying the constant exists not it's value. If your constants don't show up in your included or required files, then you probably have php safe mode turned on!


1 Answers

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.

like image 63
Eric MORAND Avatar answered Oct 03 '22 19:10

Eric MORAND