Learn PHP

PHP Constant

Constants are used to make sure a value does not change throughout the running of the script.

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.

  • Using define() function
  • Using const keyword

PHP constants follow the same PHP variable rules. For example, it can be started with letter or underscore only.

Conventionally, PHP constants should be defined in uppercase letters.

To define a constant, use the define() function, and include name of the constant, followed by the value for the constant, as shown here:

 define(name, value, case-insensitive) 

  • 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


  • Constants may only contain scalar values such as Boolean, integer, float, and string (not values such as arrays and objects).
  • Constants can be used from anywhere in your PHP program without regard to variable scope.
  • Constants are case-sensitive.

Example 1

					define("MESSAGE", "Welcome to studentsempire.com!");
					echo MESSAGE;


	      	  Welcome to studentsempire.com!	      

Example 2

					define("MESSAGE", "Welcome to W3Schools.com!", true);
					echo message;


	      	  Welcome to studentsempire.com!	      

Constants are Global

Constants are automatically global and can be used across the entire script.

Example 3

					define("MESS", "Welcome to studentsempire.com!");

					function myTest() {
					    echo MESS;


	      	  Welcome to studentsempire.com!	      

Example 4

					define("aValue", 8); 
  					print aValue; 



Example 5

					define("SecondsPerDay", 86400, true); 
					print SecondsPerDay; 
					print SECONDSperDAY; 



Example 6

The defined() function is basically the constant equivalent of isset(), as it returns true if the constant string you pass to it has been defined.

					define("SecondsPerDay", 86400, true); 
					if (defined("Secondsperday")) { 
					    // etc 

Example 7

constant() returns the value of a constant.

					define("SecondsPerDay", 86400, true); 
					$somevar = "Secondsperday"; 
					print constant($somevar); 

Example 8

			    $radius = 4; 
     	        $diameter = $radius * 2; 
			    $circumference = M_PI * $diameter; 
			    $area = M_PI * pow( $radius, 2 ); 

			    echo "A radius of " . $radius . " \n "; 
			    echo "A diameter of " . $diameter . " \n "; 
			    echo "A circumference of " . $circumference . " \n "; 
			    echo "An area of " . $area . " \n ";    

PHP Tutorials






Echo and Print Statement

Data Types



Number Convert

PHP String

php variable substitution in string

php string element

php string function

php addcslashes function

php addslashes function

php get magic quotes gpc function

php get meta tags function

php urlencode function

PHP $ and $$ Variables

PHP constant

PHP Magic Constants

PHP Static Variable

PHP Super Globals

PHP Globals

PHP Server

PHP reserved variable get

PHP reserved variable post

PHP reserved variable FILES

PHP reserved variable Cookies

PHP reserved variable Session

PHP reserved variable Request

PHP reserved variable environment

PHP Operator

PHP Arithmetic Operator

PHP Comparison Operator

PHP Incrementing and Decrementing Operators

PHP Identical Operators

PHP Logical Operators

PHP String Operators

PHP Ternary Operators

PHP Execution Operators

PHP Operator Precedence

PHP Array Operator

PHP Class

PHP Class Inheritance

PHP constructor and destructor

PHP abstract class

PHP class access control

PHP statement

PHP If statement

PHP else statement

PHP else if statement

PHP control structure statement

PHP while loop

PHP do while loop

PHP for loop

PHP for each loop

PHP break statement

PHP continue statement

PHP declare statement

PHP return statement

PHP require statement

PHP include

PHP require once

PHP include once

PHP goto statement

PHP function

PHP function parameter

PHP function scope

PHP array

PHP array operator

PHP array element loop

PHP array foreach loop

PHP array multidimensional

PHP html forms

PHP get and post method

form redirect

file inclusion

file i/o