codescracker


php

PHP Constants



« Previous Tutorial Next Tutorial »


Constants in PHP are just like variables, but in case of constants, you can not redefine/undefine once you defined a constant in PHP script.

A valid constant name in PHP, must starts with a letter or underscore (but no dollar ($) sign before the name of the constant). Constants are automatically global across the entire PHP script:

Create a PHP Constant

To create a constant in PHP, use the function named define(). Here is the general form to create PHP constant:

define(name, value, case-insensitive)

Here is the explanation of the above parameters used in creating PHP constants:

PHP Constant Example

Here is an example, creates a PHP constant with a case sensitive name:

<!DOCTYPE html>
<html>
<body>

<?php
// case-sensitive constant name
define("GREETING", "Welcome to codescracker.com");
echo GREETING;
?>

</body>
</html>

Here is the output of the above PHP script:

PHP Constants Example

Here is another example, creates a constant with a case-insensitive name:

<!DOCTYPE html>
<html>
<body>

<?php
// case-insensitive constant name
define("GREETING", "Welcome to codescracker.com", true);
echo greeting;
?>

</body>
</html>

This is the output of the above PHP script:

PHP Constants Example

PHP const Keyword

The const keyword in PHP also used to define constants in PHP. Here is an example shows how to use const keyword to define constants in PHP.

<html>
<head>
	<title>PHP const Keyword - CodesCracker</title>
</head>
<body>
<?php 
	# This example shows how to defined constants 
	# using const keyword or define() function
	# in PHP - CodesCracker
	class clss
	{
		const fullname = "CodesCracker";
		// reference from inside the class using self::
		public function fun()
		{
			return self::fullname;
		}
	}
	// reference from outside the class using
	// the class name followed by ::
	echo clss::fullname;
?>
</body>
</html>

Here is the sample output produced by the above const keyword example code in PHP:

php const keyword

Let's take another example of const keyword in PHP to define constants.

<html>
<head>
	<title>PHP const Keyword - CodesCracker</title>
</head>
<body>
<h2>PHP const Keyword</h2>
<?php 
	# const keyword example in PHP
	class userLogMsgLevel
	{
		const info = 1;
		const warning = 2;
		const error = 3;
		// you can even assign the constant as a default value
		public function log($notification, $level = self::info)
		{
			echo "Notification level ". $level.": ".$notification;
		}
	}
	$logObj = new userLogMsgLevel();
	// using default value
	$logObj->log("Info");
	echo "<br/>";
	// using variable
	$logObj->log("Warning", $logObj::warning);
	echo "<br/>";
	// using class
	$logObj->log("Error", userLogMsgLevel::error);
?>
</body>
</html>

Here is the sample output of the above const keyword example code in PHP.

php const example

Constants in PHP are Global

As already told, that constants in PHP are automatically global and you can access to these constant across the entire PHP scripts, where they are created. Here is an example, creates and defined a constant outside the function, and used that constant inside the function:

<!DOCTYPE html>
<html>
<body>

<?php
define("GREETING", "Welcome to codescracker.com");

function myTest() {
     echo GREETING;
}
  
myTest();
?>

</body>
</html>

It will display the following result:

PHP Constants Example

« Previous Tutorial Next Tutorial »


Latest 5 Posts by CodesCracker



Tools
Calculator

Quick Links
Signup - Login - Give Online Test