PHP Constants

Constants are variables
Constants are variables except that once they are defined they can’t be changed or undefined.

  • A constant is an identifier (name) for a simple value. The value cannot be changed during the script.
  • A valid constant name starts with a letter or underscore.
  • (no $ sign before the constant name).
  • Note: Unlike variables, constants are automatically global across the entire script.

Create a PHP constant

  • You can create a constant by using the define () function.
  • Use the define function to specify the name and value of a constant. (name, value, case-insensitive).
  • Many programmers use capital letters to name constants – so they stand out.
  • Note: Since a constant can not be change you do not use a $ sign before it’s name when you declare it or (use it).

name: Specifies the name of the constant
value: Specifies the value of the constant
case-insensitive: Specifies whether the constant name should be case-insensitive. Default is false
How to declare constants

define(‘male’, ‘m’) ;    // a string
constantdefine(‘PI’, 3.14159) ;  // a double constant

PHP Constant Examples

<!DOCTYPE html>
<?php// case-sensitive constant namedefine("GREETING", "Welcome to!");
echo GREETING;?>  

Output: Welcome to!

Recommended Tutorials for you

Related posts

How to fetch data in excel or generate excel file in PHP

Anuj Kumar

Array in PHP

Anuj Kumar

Implode() and Explode() Function in PHP

Anuj Kumar


Anuj Kumar

How to send Mail Using PHP

Anuj Kumar

Strings in PHP

Anuj Kumar

How to get Browser Information In PHP

Anuj Kumar

How to get current page URL in php

Anuj Kumar

How to Connect PHP with MySQL Database

Anuj Kumar

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.