How to define constant in PHP?

what is constant in PHP?

A variable whose value we cannot modify

Constant means fixed. We cannot change the value which we have fixed as fixed later.

The definition of when its occupant falls suggests that whenever you want to become a variable whose value is fixed, you do not want someone to change it accidentally. Or you change it yourself so that your program gets spoiled, then in that case we create a constant variable and we assign the value which cannot be modified later in any way nor correct. Ways nor wrongly.

Create a PHP Constant

To create a constant variable we use the define function,
Define () function is used to create constants.

The syntax is its definition. Write the value of the constant variable inside the double quotes as a comma and write the value, whichever variable is assigned to that value will write the value. Then by giving a coma, you will write the case-insensitive here, here the buy default false, here you can write two values ​​instead of case insensitive.
Either true or false, if you don’t write anything, it is considered by default falls. If you write true, then case-insensitive has been done.

Syntax:-

Define (“constant_variable”, value, case-insensitive)

Ex:-

  • define (“pi,3.14192”); <- Here “pi” is case-sensitive
  • define (“pi”, 3.141592, TRUE); <- Here “pi” is non-case-insensitive
  • define (“site”,”devopsschool.com”)

Create a constant with a case-sensitive name:

Example:

Create a constant with a case-insensitive name:

Example:-

Rule:- Some rules for writing the Constant variable

  • No need to start constant variable name with $ sign.
  • Name only starts with a letter and an underscore(_).
  • Variable name cannot start with a number.
  • It is case sensitive which implies that the variable num is lowercase is different from variable NUM in uppercase. But we can make it case insentive.
  • Constant are automatically global and can be used across the entire script.
  • Can’t use Predefined constant name e.g. PHP_VERSIon, PHP_OS etc
  • Can’t use reserved keyword. e.g. else, if etc.
Rajesh Kumar
Follow me