Skip to content

PHP7 – PHP Constants Containing Arrays

Last updated on December 13, 2015

You can define a constant by using the define() function or by using the const keyword outside a class definition as of PHP 5.3.0. Once a constant is defined, it can never be changed or undefined.

Only scalar data (Boolean, integer, float and string) can be contained in constants prior to PHP 5.6. The ability to define array constants and define a constant as a scalar expression was introduced in PHP 5.6 using the const keyword. This ability has now been applied to the define() function too in the PHP7.It is possible to define constants as a resource, but it should be avoided, as it can cause unexpected results.

define('ALLOWED_IMAGE_EXTENSIONS', ['jpg', 'jpeg', 'gif', 'png']);

If you want to get all defined constants list you can use get_defined_constants()

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments