Sunday, February 21, 2021

PHP Tutorial Part 9 : PHP - Constants

 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:

  1. Using define() function
  2. 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)  

  1. name: It specifies the constant name.
  2. value: It specifies the constant value.
  3. 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


Previous Post
Next Post

post written by:

0 Comments: