PHP | defined() function

The PHP defined() function is an inbuilt function in PHP which checks whether a constant is exists or not, in other words, defined or not.

Syntax:

bool defined($constant_name);

Parameter: This function accepts a single parameter as mentioned above and described below.



  • $constant_name: This is required parameter. It specifies the name of the constant.

Return Value: This function returns TRUE if constant exists and FALSE otherwise.

Note: This function is available for PHP 4.0.0 and newer version.

Below examples illustrate the function:

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
define("constant_key", "value for the constant key");
echo defined("constant_key");
?>

chevron_right


Output:

1

Example 2: checking with if condition after defining the constant.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
define("constant_key", "value for the constant key");
if(defined("constant_key")){
    echo "constant_key is defined";
}else{
    echo "constant_key is not defined";
}
?>

chevron_right


Output:

constant_key is defined

Example 3: checking with if condition without defining the constant.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
//define("constant_key", "value for the constant key");
if(defined("constant_key")){
    echo "constant_key is defined";
}else{
    echo "constant_key is not defined";
}
?>

chevron_right


Output:

constant_key is not defined



My Personal Notes arrow_drop_up

I am unrecognized crazy being with random thoughts

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.