PHP Constants

In this tutorial you will understand the concept of PHP Constants

  • 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.

How to create PHP constants:

define(name, value, case-insensitive)

Parameters:

  • 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

Example:

<?php
define("url", "techTutorialsOnline.com");
echo "URL of this website is: ".url;
?>

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

Example:

<?php
define("url", "techTutorialsOnline.com");

//define the function f1()
function f1()
{
echo "URL of this website is: ".url;
}

//call the function f1()
f1();
?>

This is a simple and effective tutorial of creating PHP Constants. If you have any question, ask it at our Forum Section.