Thursday, September 4, 2025
HomeLanguagesPHP | defined() function

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:




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


Output:

1

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




<?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";
}
?>


Output:

constant_key is defined

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




<?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";
}
?>


Output:

constant_key is not defined

RELATED ARTICLES

Most Popular

Dominic
32264 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6632 POSTS0 COMMENTS
Nicole Veronica
11800 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11860 POSTS0 COMMENTS
Shaida Kate Naidoo
6749 POSTS0 COMMENTS
Ted Musemwa
7025 POSTS0 COMMENTS
Thapelo Manthata
6698 POSTS0 COMMENTS
Umr Jansen
6718 POSTS0 COMMENTS