Home »
PHP »
PHP programs
PHP program to check a constant is defined or not
Here, we are going to learn how to check a constant is defined or not in PHP?
Submitted by Nidhi, on November 11, 2020
Here, we will check a constant is defined or not using the defined() function. The defined() function returns true if the given constant is defined otherwise it returns false.
Program/Source Code:
The source code to check a constant is defined or not is given below. The given program is compiled and executed successfully.
<?php
//PHP program to check a constant is defined or not.
define(PI,3.14);
if(defined("PI")==true)
print("Constant is defined");
else
print("Constant is not defined");
?>
Output:
Constant is defined
Explanation:
In the above program, we defined a constant PI initialized with 3.14. Then we checked constant PI is defined or not using the defined() method and then print the appropriate message on the webpage.
TOP Interview Coding Problems/Challenges