PHP Constants

PHP constants are identifier for a simple value. The possible datatypes of constants are boolean, integer, double, and string. Once we defined the constant, we cannot change the value of constant and cannot unset the constant.

Characteristics of constant:

  • constant is case sensitive by default
  • The name of a constant is uppercase
  • A valid constant name starts with a letter or underscore, followed by any number of letters, numbers, or underscores
  • Variables start with $, whereas constants are not start with $
  • Like global variable constant will be accessible from anywhere in the programming
  • You can get the value of constant by specifying its name

$name – Name of the constant variable
$value – Value of the constant variable
$case_insensitive – The default value is FALSE. The defined constant variable should be accessible by as it is, if you access the variable by using non case sensitive then it issues a notice. For Example: Consider following example.

Example

If you are using PHP version greater than or equal to 7.0 then it is possible to create constant variable as array().

Example

Class Constants:

If you want to create constant variable for procedural PHP coding then you can create PHP constant variable by using define() function. Otherwise you can create the constant variable by using const keyword inside the class.

Example

Click the below button to see the demo.
Demo

Please follow and like us:
error

Leave a Reply

Your email address will not be published. Required fields are marked *