PHP defined()

defined(x) function checks whether a constant defined or not. It returns TRUE if defined, or FALSE if not. if x is a variable, return FALSE.

<?PHP
	define("MAX",10000);
	if (defined("MAX")) echo "defined";  //defined
	if (defined("M_PI")) echo "defined";  //defined
	
	$max = 100;
    if (defined("max")) echo "defined";
	else echo "not defined";	//not defined
?>

The parameter must be quoted, otherwise FALSE will be returned.

<?PHP
	define("MAX",10000);
	if (defined(MAX)) echo "defined"; 
	else echo "not defined";  //not defined
	
	if (defined(M_PI)) echo "defined"; 
	else echo "not defined";  //not defined	
?>

To check whether a variable is declared or not, use function isset(). It will return TRUE if declared, otherwise FALSE. If a variable is declared as NULL, it will return FALSE too.

<?PHP
	$max = 100;
    if (isset($max)) echo "declared"; //declared
	else echo "not declared";  

	$max = NULL;
    if (isset($max)) echo "declared";
	else echo "not declared";  //not declared
?>

To check whether a function has been defined or not, use function function_exists(). It will return TRUE if defined, otherwise FALSE. The parameter should be quoted like defined().

<?PHP
    if (function_exists("preg_split")) echo "defined"; //defined
	else echo "not defined";
	
	function uppercasestr($str)
	{
		$str = strtolower($str);
		$elem = explode(" ",$str);
		$str = "";
		foreach($elem as $word)
		{
		   if ($str == "") $str .= ucfirst($word);
		   else $str .= " " . ucfirst($word);
		}
		return $str;
	}
	if (function_exists("uppercasestr")) echo "defined"; //defined
	else echo "not defined";
?>

Some PHP predefined constants are listed below. For more PHP predefined constants, please check constants.

Constant
Description
_LINE_
Line number of current file
_FILE_
Full path name of current file
_DIR_
Directory of current file
_FUNCTION_
Name of current function
_CLASS_
Name of current class
PHP_OS
Current operation system
PHP_EOL
End of Line



:: PHP Tutorials Home ::
PHP String Functions
 • concatenation • echo
 • ereg • ereg_replace
 • explode • htmlspecialchars
 • preg_match • preg_replace
 • preg_split • print,sprintf
 • regular expr. • str_replace
 • strcmp • strpos
 • strrev • strrpos
 • strtr • substr
 • substr_replace
PHP Array Functions
 • array_diff • array_flip
 • array_intersect • array_key_exists
 • array_keys • array_merge
 • array_pop • array_push
 • array_rand • array_search
 • array_splice • array_unshift
 • array_values • asort & arsort
 • count • in_array
 • ksort • shuffle
 • sort
PHP Data Types
 • array • associative array
 • date & time • number
 • class, object • regular expression
 • string • variables
PHP Loop & Conditions
 • continue & break • for loop
 • foreach • if else
 • not equal • while
PHP File System Functions
 • copy • delete, unlink
 • dirname • download url
 • file_exists • is_file
 • mkdir • read file
 • scandir • write file
PHP Popular Topics
 • ajax • clone
 • comments • constants
 • cookie • database
 • defined • die
 • form validation • gd, draw images
 • global variables • header url
 • heredoc • mail
 • pass by reference • print
 • regular expr. • sessions
 • threads • xml parse
PHP Math Functions
 • abs • cos
 • exp • floor & ceil
 • fmod • log
 • max & min • pow
 • round • sin
 • sqrt • tan
endmemo.com © 2024  | Terms of Use | Privacy | Home