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 [Last updated : March 13, 2023]

PHP - Checking If Constant is Defined?

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.

PHP code to check a constant is defined or not

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.

PHP Class & Object Programs »



Related Programs



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.