Google News
logo
PHP Constants
A constant is a name or an identifier for a simple value. A constant value cannot change during the execution of the script.  A constant name starts with a letter or underscore, followed by any number of letters, numbers, or underscores.  Constant are like variables except that one they are defined, they cannot be undefined or changed .

PHP constant: define()

Let's see the syntax of define() function in PHP.
define(name, value, case-insensitive)
  • name : specifies the constant name
  • value : specifies the constant value
  • case-insensitive : Default value is false. It means it is case sensitive by default.
Example :
<!DOCTYPE html>
<html>
<head>
	<title>PHP Constants</title>
</head>

<body>

<?php  
define("FTL","Welcome To Free Time Learn...!");  
echo FTL;  
?>  

</body>
</html>
Output :
Example :
<!DOCTYPE html>
<html>
<head>
	<title>PHP Constants</title>
</head>

<body>

<?php  
define("FTL","Welcome To Free Time Learn...!", true); //not case sensitive  
echo FTL;
echo "<br /><br />"; 
echo ftl;  
?>  

</body>
</html>
Output :
Example :
<!DOCTYPE html>
<html>
<head>
	<title>PHP Constants</title>
</head>
<?php error_reporting(0); ?>
<body>

<?php  
define("FTL","Welcome To Free Time Learn...!", false); //case sensitive  
echo FTL;
echo "<br /><br />"; 
echo ftl;  
?>  

</body>
</html>
Output :
PHP constant - const keyword

The const keyword defines constants at compile time. It is a language construct not a function.

It is always case sensitive.

Example :
<!DOCTYPE html>
<html>
<head>
	<title>PHP Constants</title>
</head>

<body>

<?php  
	const FTL="Hello const by Free Time Learning..!";  
	echo FTL;  
?>  

</body>
</html>
Output :
PHP Magic constants

Magic constants are the predefined constants in PHP which get changed on the basis of their use. They start with double underscore (__) and ends with double underscore.

A few "magical" constants are given below

Name Description
__LINE__ The current line number of the file.
__FILE__ The full path and filename of the file.
__FUNCTION__ The function name.
__CLASS__ The class name.
__METHOD__ The class method name.
Magic Constants Example :
<!DOCTYPE html>
<html>
<head>
	<title>PHP Magic Constants</title>
</head>

<body>

<?php
	echo "The Line number : ". __LINE__;
?>
<hr />
<?php
	echo  "The file name : ". __FILE__;
?>
<hr />
<?php
class Magic_Constants 
{
function FreeTimeLearn()
{
echo "Function : ". __FUNCTION__ ."<hr/>"; 
} 
function free_timt_learn()
{
echo "Class : ". __CLASS__."<hr/>";
echo "Method : ". __METHOD__ ;
				
} 
}
$object=new Magic_Constants();
$object->FreeTimeLearn();
$object->free_timt_learn();
 
?>

</body>
</html>
Output :