PHP Programming

Hands On No. 5 : Working with Constants in PHP

Working with Constants in PHP

Working with Static Variable

                                

<?php

function myFunction() {
static $x = 0;
echo $x;
$x++;
}

myFunction();
myFunction();
myFunction();
?>

Working with Constants

Syntax of Define Function
define(name, value, case-insensitive)

Without case-insensitive

<?php

define("GREETING", "Welcome to NUV");

echo GREETING;

?>

With case-insensitive

<?php

define("GREETING", "Welcome to NUV", true);

echo greeting;

?>

Working with Array of Constants

                                

<?php

define("cars", [
"Maruti",
"Hyundai",
"Toyota"
]);

echo cars[0];

?>

Creating Global Constants

                                

<?php

define("GREETING", "Welcome to NUV");

function myFunction() {
echo GREETING;
}

myFunction();

?>