PHP define/const/static

Constant is just a constant, you can't change its value after declaring. if the property should not be changed, then a constant is the proper choice. you don't use the $ symbol to declare or use them. The value must be a constant expression, not (for example) a variable, a property, a result of a mathematical operation, or a function call. Note : The variable's value can not be a keyword (e.g. self, parent and static).
Static variable or method is accessible without needing an instantiation of the class. and therefore shared between all the instances of a class. if the property allowed to be changed then use static. Static variables reduce the amount of memory used by a program.

Example 1- Static

class Foo
    public static $my_static = 'abc'; //static variable
echo Foo::$my_static;
//output : abc

Example 2 - Static

Static local variable in a function that is declared only once on the first execution of a function.

function static_test()
   static $i = 0;
   echo "function has been called ".$i." times";
//function has been called 1 times
//function has been called 2 times

define vs const

  • const defines constants at compile time
  • define defines them at run time
  • define I use for global constants.
  • const I use for class constants.
  • const is a language construct
  • const is faster than define
// Works as of PHP 5.3.0
const CONSTANT = 'Hello World';


// Works as of PHP 5.6.0
const ANOTHER_CONST = CONSTANT.'; Goodbye World';

const ANIMALS = array('dog', 'cat', 'bird');
echo ANIMALS[1]; // outputs "cat"

// Works as of PHP 7
define('ANIMALS', array(
echo ANIMALS[1]; // outputs "cat"