How to define constant in PHP

Constants are like other variables but once they are defined they cannot be changed or undefined.

Things to remember about PHP constant: Constants are like variables except that once they are defined they cannot be changed or undefined.


Syntax: define(name, value[, $boolean])
name– $string
value– $scalar
$boolean– [optional] default: FALSE, case-sensitive

Define a constant, a set value that is assigned globally, making it available to
functions and classes without passing them directly as an argument. 

Examples:
<?php
define("HELLO""I Love PHP");
echo HELLO; // output I Love PHP

define("HELLO""I Love PHP");
echo hello; // output I Love PHP

?>

Note: PHP Constants are automatically global and can be used across the entire script.

The example below show, how we can used a constant inside a function, though it is defined outside the function:

<?php
define("HELLO""I Love PHP");
function greetMe() {
    echo HELLO;
}

greetMe();
?>


Comments

Popular posts from this blog

What Is Kotlin - About Kotlin