<?php
define("emp_name","Kumar D");
echo constant("emp_name");
//or
echo emp_name;
?>
<?php
define("PI",3.14);
echo PI;
echo constant("PI");
?>
//define(name,value);
define("PI",3.1419);
echo constant($constant_name);
<?php
class MyClass
{
const CONSTANT = 'constant value';
function showConstant() {
echo self::CONSTANT . "
";
}
}
echo MyClass::CONSTANT . "
";
$classname = "MyClass";
echo $classname::CONSTANT . "
";
$class = new MyClass();
$class->showConstant();
echo $class::CONSTANT."
";
?>
class Human {
const TYPE_MALE = 'm';
const TYPE_FEMALE = 'f';
const TYPE_UNKNOWN = 'u'; // When user didn't select his gender
.............
}
<?php
#Syntax to define a constant is Define("Name","Value","Case-insensitive")
#For case-insensitive, sensitive is default
define("Tau","6.28318530718","true");
echo tAU;
#Fun fact: Tau = Pi x 2
?>
<?php
define("A", 100);
define("B", 200);
echo A;
echo "<br>";
echo B;
?>
<?php
define("GREETING", "Welcome to my profile");
echo GREETING;
?>
if (...) {
const FOO = 'BAR'; // Invalid
}
// but
if (...) {
define('FOO', 'BAR'); // Valid
}