Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
define() function in PHP
The define() function defines a constant in PHP. Constants are global variables whose values cannot be changed during script execution.
Syntax
define(const_name, value, case_insensitive)
Parameters
const_name − The name of the constant (string).
value − The value of the constant (scalar value).
case_insensitive − Optional. Whether the constant name should be case-insensitive (default: false).
Return Value
The define() function returns true on success or false on failure.
Examples
Basic Example
The following example defines a constant and accesses it directly ?
<?php
define("MESSAGE", "Hello, World!");
echo MESSAGE;
?>
Hello, World!
Using constant() Function
You can also retrieve constant values using the constant() function ?
<?php
define("PI", 3.14159);
echo "Value of PI: " . constant("PI");
?>
Value of PI: 3.14159
Case-Insensitive Constants
This example creates a case-insensitive constant ?
<?php
define("GREETING", "Good Morning!", true);
echo greeting; // Works with lowercase
?>
Good Morning!
Conclusion
The define() function is essential for creating constants in PHP. Use it to define values that won't change throughout script execution, improving code maintainability and performance.
