PHP – Constants Types

PHP constants are name or identifier that can’t be changed at the time of the execution of the script. we used PHP constants in 2 ways:

  1. Using define() function
  2. Using const keyword

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.

PHP constant: define()

Let’s see the syntax of define() function in PHP.

define(name, value, case-insensitive) 

  1. name: specifies the constant name
  2. value: specifies the constant value
  3. case-insensitive: Default value is false.
<?php
define("MESSAGE","Hello JavaTpoint PHP");  
echo MESSAGE;
?>

Output:
Hello JavaTpoint PHP

Const Keyword

The const keyword defines constants at compile time. It is a language construct not a function.

It is a bit faster than define().

It is always case sensitive.

<?php
const MESSAGE="Hello const by JavaTpoint PHP";  
echo MESSAGE;  
?>

Output:

Hello const by JavaTpoint PHP

Leave a Reply

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

*

*

*