PHP Constants
PHP constants are name or identifier that can't be changed during the execution of the script except for magic constants, which are not really constants. PHP constants can be defined in 2 ways:
- Using define() function
- Using const keyword
Constants are similar to the variable except once they defined, they can never be undefined or changed. They remain constant across the entire program. PHP constants follow the same PHP variable rules. For example, it can be started with a letter or underscore only.
Conventionally, PHP constants should be defined in uppercase letters.
Note: Unlike variables, constants are automatically global throughout the script.
PHP constant: define()
Use the define() function to create a constant. It defines constant at run time. Let's see the syntax of the define() function in PHP.
define(name, value, case-insensitive)
- name: It specifies the constant name.
- value: It specifies the constant value.
- case-insensitive: Specifies whether a constant is case-insensitive. The default value is false. It means it is case sensitive by default.
Let's see the example to define PHP constant using define().
File: constant1.php
0 Comments: