Php Constants

Sujitha

 Php Constants

         In PHP, constants are identifiers (names) that represent a fixed value that cannot be changed during the execution of a script.

        Constants can be useful for improving code readability, avoiding magic numbers, and ensuring that certain values are not accidentally modified.

Create a php constants

To create a constant in PHP, you use the define() function.

Syntax

define("name", value, case_insensitive);

Parameters

name : The name of the constant, usually written in uppercase letters.

value: The value assigned to the constant.

case_insensitive: Optional. If set to true, the constant's name will be case-insensitive. Default is false.

Php constants array

The array itself, though, cannot be redefined once defined using the define() function.

<?php
define("FRUITS", ["apple", "banana", "orange"]);
echo echo FRUITS[0];
?>

 // Outputs: apple

Constants are global


     it is available and accessible from anywhere in your script, regardless of the scope.

<?php
define("my constants", "Hello world!");

function myFunction() {
    echo my constants();
}
myFunction();
?>

// output  Hello world












Our website uses cookies to enhance your experience. Learn More
Accept !

GocourseAI

close
send