php

Define constant in PHP

<?php
    define(const_name, const_value); //SYNTAX
    define("month", "september");
?>

Constants can only be changed when initialize. In PHP you can define a constant by using the define() function which takes two required parameters constant name and constant value and one optional parameter case insensitive.

A constant is global means it can be accessed anywhere from the script once initialized and you also don't need to add the '$' keyword before the variable name to define a constant.

Example:

<?php
   define("firstname", "John");
   echo firstname;

   define("fruits", ["banana", "apple", "papaya"]);
   echo fruits[1];
?>
Was this helpful?