WikiGalaxy

Personalize

PHP Constants

What are PHP Constants?

Constants in PHP are like variables, but once they are defined, they cannot be changed or undefined. They are useful for storing fixed values that should not change during the execution of the script.

Defining Constants

Constants are defined using the define() function, which takes two arguments: the name of the constant and its value.


<?php
define("SITE_NAME", "WikiGalaxy");
echo SITE_NAME;
?>
    

Console Output:

WikiGalaxy

Case Sensitivity in Constants

Are Constants Case-Sensitive?

By default, constants are case-sensitive. However, you can make them case-insensitive by passing true as the third argument in the define() function.


<?php
define("PI", 3.14159, true);
echo pi;
?>
    

Console Output:

3.14159

Using Constants in Expressions

Can Constants be Used in Expressions?

Yes, constants can be used in expressions. They are often used to define configuration values that are used throughout a script.


<?php
define("TAX_RATE", 0.08);
$price = 100;
$total = $price + ($price * TAX_RATE);
echo $total;
?>
    

Console Output:

108

Predefined Constants

What are Predefined Constants?

PHP provides a large number of predefined constants to any script which it runs. They provide information about the PHP environment and configuration.


<?php
echo PHP_VERSION;
echo PHP_OS;
?>
    

Console Output:

8.0.2

Linux

Magic Constants

What are Magic Constants?

Magic constants are predefined constants in PHP, but unlike other constants, their values change depending on where they are used.


<?php
echo __LINE__;
echo __FILE__;
?>
    

Console Output:

3

/path/to/script.php

Constant Arrays

Can Constants be Arrays?

In PHP 7.0 and later, you can define array constants using the define() function, which allows for more complex constant values.


<?php
define("COLORS", ["Red", "Green", "Blue"]);
echo COLORS[0];
?>
    

Console Output:

Red

Scope of Constants

Are Constants Global?

Constants are automatically global and can be used across the entire script, regardless of where they have been defined.


<?php
define("GREETING", "Hello, World!");

function greet() {
  echo GREETING;
}

greet();
?>
    

Console Output:

Hello, World!

Constants vs Variables

How do Constants Differ from Variables?

Unlike variables, constants do not start with a dollar sign ($) and cannot be changed once they are set. They are also automatically global.


<?php
define("MAX_USERS", 100);
$maxUsers = 50;
echo MAX_USERS;
echo $maxUsers;
?>
    

Console Output:

100

50

Best Practices for Constants

How to Use Constants Effectively?

Use constants for values that are unlikely to change. This makes your code easier to maintain and understand. Always use uppercase letters for naming constants to distinguish them from variables.


<?php
define("DB_HOST", "localhost");
define("DB_USER", "root");
define("DB_PASS", "password");

echo DB_HOST;
echo DB_USER;
echo DB_PASS;
?>
    

Console Output:

localhost

root

password

logo of wikigalaxy

Newsletter

Subscribe to our newsletter for weekly updates and promotions.

Privacy Policy

 • 

Terms of Service

Copyright © WikiGalaxy 2025