PHP Constants

 A PHP cosntant is an identifier  which value cannot be changed during the script.

A valid constant name start with letter or underscore(no other special character or number).

Syntax:

define(name, value, case-insensitive)


Example:

<?php

define("GREETING", "Welcome to W3Schools.com!");

echo GREETING;

?>


Array constant in PHP:

<?php

define("cars", [

  "Alfa Romeo",

  "BMW",

  "Toyota"

]);

echo cars[0];

?>


Constants are global in PHP you can use it anywhere see below example:

<?php

define("GREETING", "Welcome to W3Schools.com!");


function myTest() {

  echo GREETING;

}

 

myTest();

?>


Comments

Popular posts from this blog

PHP Variables

PHP Comments

PHP Operators