Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

PHP 5 constant


May 10, 2021 PHP


Table of contents


PHP 5 constant


After the constant value is defined, it cannot be changed anywhere else in the script.


PHP constant

A constant is an identifier of a simple value. The value cannot be changed in the script.

A constant consists of letters, underscores, and numbers, but numbers cannot appear as initials. (Constant names do not require a $ modifier).

Note: Constants can be used throughout the script.


Set the PHP constant

Set the constant, using the define() function, which has the following syntax:

bool define ( string $name , mixed $value [, bool $case_insensitive = false ] )

The function has three parameters:

  • constant_name: required parameters, constant names, that is, markers.
  • value: Required parameters, constant values.
  • case_insensitive: Optional parameter, which is case insensitive if set to TRUE. The default is case sensitive.

In the following example, we create a case-sensitive constant with a constant value of "Welcome w3cschool.cn!"

<?php
define("GREETING", "Welcome to w3cschool.cn!");
echo GREETING;
?>

Try it out . . .

In the following example, we create a case-insensiable constant with a constant value of "Welcome w3cschool.cn!"

<?php
define("GREETING", "Welcome to w3cschool.cn!", true);
echo greeting;
?>

Try it out . . .

Tip: When using PHP, be aware of the difference between the constant and the variable.