Arman H
Arman H

Reputation: 1754

How to define constant in PHP

I do not understand how this code work. displaying code below , in this code i defined a constant and the value is 10 but it display the output "constant is not defined".

why?

define('HELLO', 10);        
if(defined(HELLO))        
{        
    echo "Constant is defined";        
}        
else        
{        
    echo "Constant is not defined";        
}        

?>

Upvotes: 1

Views: 65

Answers (3)

Rahul Gupta
Rahul Gupta

Reputation: 1041

Even you can follow this way of checking and defining constant in PHP.

defined('CONSTANT') or define('CONSTANT', 'SomeDefaultValue');

For your question, you have made a mistake of quote. follow as :

if(defined('HELLO')){
  echo "Constant is already defined";
}else{
  echo "Constant is not defined, you can define it.";
}

https://www.php.net/manual/en/function.defined.php#84439

Upvotes: -1

user12226277
user12226277

Reputation:

Define Constant in PHP:

define("HELLO", 10);

If condition needs to change, then: defined("HELLO")

Upvotes: 0

Ben Shoval
Ben Shoval

Reputation: 1750

change:

defined(HELLO)

to:

defined('HELLO')

http://php.net/manual/en/function.defined.php

Upvotes: 4

Related Questions