0% found this document useful (0 votes)
40 views2 pages

PHP Cosntantas

In PHP, a constant is an immutable identifier for a value, defined using the define() function, and is case-sensitive by default. Constants differ from variables in that they cannot be redefined or undefined once set, and they do not require a dollar sign prefix. Valid constant names must start with a letter or underscore and can include numbers and underscores, while invalid names cannot start with a number.

Uploaded by

mdhasan.ansari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views2 pages

PHP Cosntantas

In PHP, a constant is an immutable identifier for a value, defined using the define() function, and is case-sensitive by default. Constants differ from variables in that they cannot be redefined or undefined once set, and they do not require a dollar sign prefix. Valid constant names must start with a letter or underscore and can include numbers and underscores, while invalid names cannot start with a number.

Uploaded by

mdhasan.ansari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

A constant in PHP is a name or an identifier for a simple value.

A constant
value cannot change during the execution of the PHP script.

 By default, a PHP constant is case-sensitive.


 By convention, constant identifiers are always uppercase.
 A constant name starts with a letter or underscore, followed by any number
of letters, numbers, or underscore.
 There is no need to write a dollar sign ($) before a constant, however one
has to use a dollar sign before a variable.

1.1 Examples of Valid and Invalid Constant Names in PHP

Here are some examples of valid and invalid constant names in PHP −

// Valid constant names


define("ONE", "first thing");
define("TWO2", "second thing");
define("THREE_3", "third thing");
define("__THREE__", "third value");

// Invalid constant names


define("2TWO", "second thing");

1.2 Difference between Constants and Variables in PHP

 Constants cannot be defined by simple assignment; they can only be defined


using the define() function.
 Constants may be defined and accessed anywhere without regard to variable
scoping rules.
 Once the Constants have been set, they may not be redefined or undefined.

Explore our latest online courses and learn new skills at your own pace.
Enroll and become a certified expert to boost your career.

1.3 Defining a Named Constant

The define() function in PHP library is used to define a named constant at


runtime.

define(string $const_name, mixed $value, bool $case = false): bool


1.3.1 Parameters
 const_name − The name of the constant.
 value − The value of the constant. It can be a scalar value (int, float, string,
bool, or null) or array values are also accepted.
 case − If set to true, the constant will be defined case-insensitive. The
default behavior is case-sensitive, i.e., CONSTANT and Constant represent
different values.

The define() function returns "true" on success and "false" on failure.

1.3.2 Example 1
The following example demonstrates how the define() function works −

Open Compiler

<?php
define("CONSTANT", "Hello world.");

echo CONSTANT;
// echo Constant;
?>
The first echo statement outputs the value of CONSTANT. You will get the
following output −
Hello world.

You might also like