PHP | Constants
Constants are either identifiers or simple names that can be assigned any fixed values. They are similar to a variable except that they can never be changed. They remain constant throughout the program and cannot be altered during execution. Once a constant is defined, it cannot be undefined or redefined. Constant identifiers should be written in upper case following the convention. By default, a constant is always case-sensitive, unless mentioned. A constant name must never start with a number. It always starts with a letter or underscores, followed by letter, numbers or underscore. It should not contain any special characters except underscore, as mentioned.
Creating a PHP Constant
The define() function in PHP is used to create a constant as shown below:
Syntax:
define(name, value, case_insensitive)
The parameters are as follows:
- name: The name of the constant.
- value: The value to be stored in the constant.
- case_insensitive: Defines whether a constant is case insensitive. By default this value is False, i.e., case sensitive.
Example:
<?php // This creates a case-sensitive constant define("WELCOME", "GeeksforGeeks"); echo WELCOME, "\n"; // This creates a case-insensitive constant define("HELLO", "GeeksforGeeks", true); echo hello; ?> |
Output:
GeeksforGeeks GeeksforGeeks
constant() function
Instead of using the echo statement ,there is an another way to print constants using the constant() function.
Syntax
constant(name)
Example:
<?php define("WELCOME", "GeeksforGeeks!!!"); echo WELCOME, "\n"; echo constant("WELCOME"); // same as previous ?> |
Output:
GeeksforGeeks!!! GeeksforGeeks!!!
Constants are Global: By default, constants are automatically global, and can be used throughout the script, accessible inside and outside of any function.
Example:
<?php define("WELCOME", "GeeksforGeeks"); function testGlobal() { echo WELCOME; } testGlobal(); ?> |
GeeksforGeeks
Constants vs Variables
- A constant, once defined can never be undefined but a variable can be easily undefined.
- There is no need to use dollar sign($) before constants during assignment but while declaring variables we use a dollar sign.
- A constant can only be defined using a define() function and not by any simple assignment.
- Constants dont need to follow any variable scoping rules and can be defined anywhere.
This article is contributed by Chinmoy Lenka. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Recommended Posts:
- PHP | Defining Constants
- How to insert HTML content into an iFrame using jQuery?
- How to get the child node index in JavaScript?
- How to use jQuery in Angular ?
- How to restrict input box to allow only numbers and decimal point JavaScript?
- How to get the height of device screen in JavaScript ?
- How to get the elements of one array which are not present in another array using JavaScript?
- How do you get the length of a string in JQuery?
- How to determine which element the mouse pointer move over using JavaScript ?
- How to add innerHTML to print page?
- How to get the type of DOM element using JavaScript?
- How to disable dragging an image from an HTML page using JavaScript/jQuery ?
- How to check whether an object exists in javascript ?
- How to check for IP address using regular expression in javascript?



