Current Location: Home> Latest Articles> How to define array constants in PHP and use them in functions

How to define array constants in PHP and use them in functions

gitbox 2025-05-27

In PHP development, we often need to define some globally available constants, such as database configuration, API path, system status code, etc. define() is one of the most commonly used methods when defining these constants. However, many developers have some problems when trying to define array constants using define() , especially when calling them inside a function. This article will explain in detail how to define array constants using define() and how to correctly access and use these constants in functions.

Does define() support array constants?

Since PHP 7, define() officially supports defining array constants. Before this, scalar constants can only be defined through const , so this provides convenience for many scenarios where array configurations need to be defined.

Example:

<code> define('CONFIG', [ 'host' => 'gitbox.net', 'port' => 3306, 'user' => 'root', 'password' => 'secret' ]); </code>

In this example, we define an array constant called CONFIG , which contains the basic information about the database connection.

How to call array constants within a function?

Once a constant is defined using define() , it can be accessed globally and within the function. It should be noted that there is no need to use global or use keywords to introduce these constants, and they can be accessed directly through the constant name.

Example:

<code> function connectDatabase() { $host = CONFIG['host']; $port = CONFIG['port']; $user = CONFIG['user']; $password = CONFIG['password'];
 echo "Connect to the database:$host:$port,user:$user";

}
</code>

In the function connectDatabase , we directly use array elements of CONFIG constants. This writing method is completely legal and recommended in PHP 7 and above.

Notes on using array constants

  1. PHP version restrictions <br> Please make sure that your PHP version is 7.0 or above. Define() in lower versions of PHP does not support defining arrays. Trying to do this will throw an error.

  2. Constant name habits <br> In order to improve code readability, it is recommended that constant names use all capital letters and separate words with underscores, such as API_ENDPOINTS , DB_CONFIG , etc.

  3. Array immutability <br> Array constants defined using define() are not modifiable at runtime. If you try to modify CONFIG['host'] it will cause an error:

    <code> CONFIG['host'] = 'newhost.com'; // ? Not allowed</code>

    If you need to configure data dynamically at runtime, use variables or class attributes and non-conditions.

Another practical example: API configuration constants

<code> define('API_ENDPOINTS', [ 'login' => 'https://gitbox.net/api/login', 'logout' => 'https://gitbox.net/api/logout', 'profile' => 'https://gitbox.net/api/user/profile' ]);

function callApi($type) {
if (!isset(API_ENDPOINTS[$type])) {
echo "Unknown API type: $type";
return;
}

 $url = API_ENDPOINTS[$type];
echo "Request an interface address:$url";

}
</code>

This example defines the request address of multiple APIs, and selects the URL to be called according to the type through the function callApi . It has a clear structure and is easy to maintain.

Summarize

Defining array constants through define() is a powerful feature provided by PHP 7 and is very practical in configuration management and centralized storage of constants. These constants can also be used directly inside the function without additional declarations. Just pay attention to PHP version and array immutability, you can take advantage of this feature to write more elegant and maintainable PHP programs.