Learn PHP Programming

A backend server scripting language, and a powerful tool for making dynamic and interactive Web pages. Learn each topic with examples and output.

PHP Introduction
Control Structures
Php Forms
Php Advanced

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.

To create a constant, use the define() function.

Syntax

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. Note: Defining case-insensitive constants was deprecated in PHP 7.3. PHP 8.0 accepts only false, the value true will produce a warning.

<?php
define("GREETING", "Welcome to Analyze Code!");
echo GREETING;
?>
Welcome to Analyze Code

n PHP7, you can create an Array constant using the define() function.

<?php
define("cars", [
  "Alfa Romeo",
  "BMW",
  "Toyota"
]);
echo cars[0];
?>
Alfa Romeo

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

<?php
define("GREETING", "Welcome to Analyze Code!");

function myTest() {
  echo GREETING;
}
 
myTest();
?>
Welcome to Analyze Code

Now that you have done constants in Php, let's learn Maths and its function in php


Share this page on :

=