PHP- Constants

  • Constants are the identifiers used in the case of simple values.
  • Variables with constant returning value is known as constants.
  • The constant values are stored on a variable and return using some functions.
  • If the constant is not defined then it return a null value.
  • Constant values are not changed during the execution of the program.
  • The name of the constant is basically start with an alphabet or underscore.
  • There is no $ symbol used for initializing any constant.
  • Constants used to define scalars only.
  • Constants are global because once a constant define then we can able to access that constant from any position of that program.
  • define() is the only function used to define a constant.

Syntax [ constant ]

 

Example 1 [ constant ]

Output

 

Basically constants are case-sensitive by default. But we can change this property of the constant.

Syntax [ constant (case-insensitive) ]

 

Example 2 [ constant (case-insensitive) ]

Output

 

The constants are basically global, that is in case of function all variables must initialize inside the function if the variables are used inside that function, but in case of constants we can able to define the constant outside the function and use same constant inside that function.

The point is that the constants are global. it can access from everywhere.

Example 3 [ constant (using function) ]

Output

 

Summary
PHP - Constants
Article Name
PHP - Constants
Description
This article is about php constants and how it should useful in php application.
Author
Publisher Name
Jees K Denny
Publisher Logo

Leave a Reply