Constants in php

In this lesson we will learn about the term constants, their usage and declaration. So lets first understand about constant.

Definition : A constant is defined as an identifier (or name) for a value which once declared remains same or we can say can not be changed in the script.

Now we will see how to declare a constant. A constant can be declared using define() function.

define() takes three parameter

  • First parameter is the name of the constant term which you will use in your script.
  • Second parameter is the value, which you want to assign to the constant.
  • Third parameter says that i can be used for case sensitive if required, Default is false if you do not provide third parameter in define function.

A constant name must starts with letter or underscore and do not use $ sign before constant name to declare or to access the constant.

Syntax :  define(name, value, case-insensitive)

Constant declaration

 

PHP constants are global in its nature, means you can access it anywhere inside your entire script. you can also access it inside the user defined function. let’s take an example to understand this. 

Constants are globals

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *