Learn with Yasir
Share Your Feedback

PHP Basics Tutorial – Constants in PHP


Learn what constants are in PHP and how to define them using the define() function. This guide covers constant usage, syntax, and case sensitivity with examples.

What is a Constant?

  • A constant is a value that cannot be changed once set, unlike variables.
  • Constants are defined using define().

Example #1:

<?php
define("SITE_NAME", "My Awesome Site");
echo SITE_NAME;  // Output: My Awesome Site
?>

Example #2: Defines a named constant

<!DOCTYPE html>
<html lang="en">
    <head>
        <title>PHP - Contants</title>
    </head>
    <body>
        <?php
        // define(name, value, case-insensitive)
        // case-insensitive: Specifies whether the constant name should be case-insensitive. Default is false
        define("GREETING", "Welcome to Department of CS & IT!");

        function myTest() {
            echo GREETING;
        }
        myTest();
        ?> 
    </body>
</html>

References

[1] “define,” PHP Manual. [Online]. Available: https://www.php.net/manual/en/function.define.php. [Accessed: Apr. 29, 2025].


🧠 Practice & Progress

Explore More Topics

PHP Basics

More ...