<!DOCTYPE html>
<html>
<head>
<title>PHP Constants</title>
</head>
<body>
<?php
define("FTL","Welcome To Free Time Learn...!");
echo FTL;
?>
</body>
</html>
<!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>
<!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>
The const keyword defines constants at compile time. It is a language construct not a function.
It is always case sensitive.
<!DOCTYPE html>
<html>
<head>
<title>PHP Constants</title>
</head>
<body>
<?php
const FTL="Hello const by Free Time Learning..!";
echo FTL;
?>
</body>
</html>
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. |
<!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>