Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Constants

ETL LABS PVT LTD – PHP 35


constant() function

Rules for defining constant


Constants are defined using PHP’s define( )
function, which accepts two arguments:
The name of the constant, and its value.

Constant name must follow the same rules as 1


variable names, with one exception the ―$‖
prefix is not required for constant names.

Syntax

<?php
define('ConstName', 'value');
?>

ETL LABS PVT LTD – PHP 36


Examples

Valid and invalid constant


names−

ETL LABS PVT LTD – PHP 37


Differences between
constants and variables
1. There is no need to write a dollar sign ($)
before a constant, where as in Variable one
has to write a dollar sign.

3 2. Constants cannot be defined by simple


assignment, they may only be defined using
the define() function.

3. Constants may be defined and accessed


anywhere without regard to variable scoping
rules.

4. Once the Constants have been set, may not


be redefined or undefined.

ETL LABS PVT LTD – PHP 38

You might also like