PHP Constants

A constant once defined, cannot be undefined. A valid constant name starts with a letter or underscore (no $ sign before the constant name). PHP constants use the define() function for creating a constant.

Constant name starts with a letter or underscore, but they can’t be considered as a variable, since constant name once defined can’t be changed.

The define() function is used for creating a constant. Here’s the syntax:

define(name, val, case-insensitive)

PHP define function syntax

Here,

  • name is the constant name
  • val is the value of the constant
  • case-insensitive is for constant name insensitive.

The following is an example:

<!DOCTYPE html>
<html>
<body>

<?php
   define("RANKING", "ICC Rankings for Team!");
   echo RANKING;
?> 

</body>
</html>

Here’s the output,

PHP constants define function

In the above example, we haven’t mentioned the case section, since the default is false. If you want a case-insensitive name, use true i.e

define("RANKING", "ICC Rankings for Team!", true);
echo ranking;

We used the define() function since it is used for creating a constant.

PHP Data types
PHP Operators
Studyopedia Editorial Staff
contact@studyopedia.com

We work to create programming tutorials for all.

No Comments

Post A Comment