PHP Constant Arrays

In PHP, you can define constant arrays using the define() function. A constant array is a collection of values that cannot be changed during the execution of the script.

Here’s an example of how to define a constant array in PHP:

define('FRUITS', array(

Now, the constant FRUITS has been defined as an array with the values apple, banana , orange , and pear . You can use this constant array throughout your code and it will always have the same values.

Once a constant array is defined, you can access its values like this:

echo FRUITS[0]; // Output: apple
echo FRUITS[1]; // Output: banana
echo FRUITS[2]; // Output: orange
echo FRUITS[3]; // Output: pear

Note that constant array elements are traditionally written in uppercase letters to differentiate them from variables.

It’s important to note that constant arrays cannot be modified during the execution of the script. If you try to modify the values of a constant array, you will receive an error. Also, constant arrays are global and can be accessed from any part of the script.

Scroll to Top