PHP Basic Tutorial
MySQL Connection
Constants are similar to variables except that, once they are defined, their values cannot be changed during the execution of the script. Constants are useful for defining values that are used throughout a program but should remain the same.
define()
Function:define()
function is used to create a constant.$
symbol when being defined or used.Syntax:
define(name, value)
Try it yourself
Note
Constant name is case-sensitive.
2. Using const
Keyword (More common for defining class constants):
Constants can also be defined using the const
keyword
Try it yourself
Note
const
vs. define()
const
cannot be created inside another block scope, like inside a function or inside an if
statement.define
can be created inside another block scope.From PHP7, you can create an Array constant using the define()
function.
Try it yourself
Constants are automatically global and can be used across the entire script, including within functions and methods.
Try it yourself
Note
Only defined constants are have Global scope.