Tags
PHP
Asked 2 years ago
20 May 2021
Views 268
samir

samir posted

constant() Function in PHP

what is use of constant() Function in php ?
and how to use constant() Function in php ?
hanuman

hanuman
answered Nov 30 '-1 00:00

constant() Function used as getter function to get the constant

in PHP , define the constant with the define function


define("IAMCONSTANT","Yes");


now you can get it by two way :
1.either use direct name of constant like this :

     echo IAMCONSTANT; 
   

it will print Yes

2. Use constant() Function :

     echo constant("IAMCONSTANT"); 
   

it will print Yes

if constant is not defined than it will return null

     echo constant("IAMNOTCONSTANT"); 
   

"IAMNOTCONSTANT" is not defined so it will print nothing

if you want to avoid the empty print or any check about constant is exist or not.
you can use defined method to do so

     if(!defined("IAMNOTCONSTANT")){
        //not defined constant so lets define it 
        define("IAMNOTCONSTANT","NO");
     }
   


Post Answer