67

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

Jesse Nickles
  • 548
  • 1
  • 4
  • 17
ryanc1256
  • 835
  • 1
  • 7
  • 14

6 Answers6

86

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.

kenorb
  • 118,428
  • 63
  • 588
  • 624
Eric MORAND
  • 6,075
  • 3
  • 24
  • 31
83

Use defined() function, for example:

if (defined('VAR_NAME')) {
    // Something
}
kenorb
  • 118,428
  • 63
  • 588
  • 624
Tom Walters
  • 13,978
  • 5
  • 53
  • 71
41

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;
}
?>
kenorb
  • 118,428
  • 63
  • 588
  • 624
Niklas
  • 18,855
  • 28
  • 114
  • 153
  • Incorrect; OP's "variables" weren't variables - visible from the code from the first revision, clarified in text later on. – John Dvorak Jan 20 '13 at 21:22
  • You first questioned about how to check if a variable exist .. and then you changed it to a constant. ... It would be polite if you don't vote down my post ... – Niklas Jan 20 '13 at 21:27
  • I didn't - I'm not the asker. Note that votes are about correctness as well as effort. Note that deleting answers is free and reclaims your points, and you would have to do it anyways (or edit until it's correct). – John Dvorak Jan 20 '13 at 21:31
  • Note that link-only answers are not welcome here anyways (also note you've linked to the german translation by accident) – John Dvorak Jan 20 '13 at 21:32
23

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

SherylHohman
  • 12,507
  • 16
  • 70
  • 78
Svetoslav Marinov
  • 1,390
  • 12
  • 11
5

I take it you mean CONSTANTS not variables! the function is defined();

see here: defined

Zac
  • 1,545
  • 2
  • 11
  • 15
0

With defined you'll have to do something like that:

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:

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

Marlon
  • 1,283
  • 2
  • 15
  • 31