
- PHP 7 Tutorial
- PHP 7 - Home
- PHP 7 - Introduction
- PHP 7 - Performance
- PHP 7 - Environment Setup
- PHP 7 - Scalar Type Declarations
- PHP 7 - Return Type Declarations
- PHP 7 - Null Coalescing Operator
- PHP 7 - Spaceship Operator
- PHP 7 - Constant Arrays
- PHP 7 - Anonymous Classes
- PHP 7 - Closure::call()
- PHP 7 - Filtered unserialize()
- PHP 7 - IntlChar
- PHP 7 - CSPRNG
- PHP 7 - Expectations
- PHP 7 - use Statement
- PHP 7 - Error Handling
- PHP 7 - Integer Division
- PHP 7 - Session Options
- PHP 7 - Deprecated Features
- PHP 7 - Removed Extensions & SAPIs
- PHP 7 Useful Resources
- PHP 7 - Quick Guide
- PHP 7 - Useful Resources
- PHP 7 - Discussion
Constant arrays in PHP 7
In PHP 5.6, we could only initialize the constant arrays using the const keyword. For example,
conststudent_rollnos = [11,12,13,14,15];
In PHP 7, we can initialize constant arrays using the definefunction. For example,
define('subjects', ['Computer', 'operating system', 'networking', 'PHP 7','software engineering']);
Here, subjectsis the constant array name and the subjects constant array names are 'Computer', 'operating system', 'networking', 'PHP 7', and 'software engineering'.
Constant array index starts from 0, like any other array. Therefore, the computer elements will be at 0 indexes and the operating system will be at 1 index and, so on.
PHP 7 constant arrays Example
<?php const student_rollnos = [11,12,13,14,15]; define('subjects', ['Computer', 'operating system', 'networking', 'PHP 7','software engineering']); print_r(student_rollnos); print_r(subjects); ?>
Output
The output for the above PHP 7 program will be −
Array ( [0] => 11 [1] => 12 [2] => 13 [3] => 14 [4] => 15 ) Array ( [0] => Computer [1] => operating system [2] => networking [3] => PHP 7 [4] => software engineering )
Explanation: In the above example, we used the define() function to declare an array name as subjects and 5 subjects name constants whose values cannot be changed.
- Related Articles
- Types of constant arrays in PHP 7
- FILTER_VALIDATE_BOOLEAN constant in PHP
- FILTER_VALIDATE_EMAIL constant in PHP
- FILTER_VALIDATE_FLOAT constant in PHP
- FILTER_VALIDATE_INT constant in PHP
- FILTER_VALIDATE_IP constant in PHP
- FILTER_VALIDATE_REGEXP constant in PHP
- FILTER_VALIDATE_URL constant in PHP
- FILTER_SANITIZE_EMAIL constant in PHP
- FILTER_SANITIZE_ENCODED constant in PHP
- FILTER_SANITIZE_MAGIC_QUOTES constant in PHP
- FILTER_SANITIZE_NUMBER_FLOAT constant in PHP
- FILTER_SANITIZE_NUMBER_INT constant in PHP
- FILTER_SANITIZE_SPECIAL_CHARS constant in PHP
- FILTER_SANITIZE_STRING constant in PHP

Advertisements