CONSTANTS in PHP

CONSTANTS must be defined and can’t be changed throughout the program like $variables can.

CONSTANTS are global and therefore can be accessed from FUNCTIONS as well.

<?php

     define('GREETING', 'Hello world!');

     function hello () {
          echo GREETING;
     }

     hello(); // this will echo out content of our constant GREETING

Other facts about constants:

  • must be always in CAPITAL LETTERS;
  • constants can be access everywhere (even within functions) without using GLOBAL VARIABLES or defining global;
  • $variables can be assigned to constants e.g $greetings = GREETING;
  • constants will trun into strings when used with “” or ”, therefore constants must always be used without “” and ”;