Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

81
Views
PHP constant as an array

Is there a possibility to make a group of constants and get them exactly like an array instead of writing each constant independently ?

something like

echo MYCONST[0]; or 
echo MYCONST['name'];
about 4 years ago · Santiago Trujillo
2 answers
Answer question

0

What you are looking for is called Constant array :

it is now possible to do this with define() but only for PHP7 . However you can do this on PHP5.6 by using the const keyword and it is not possible to perform this in lower PHP versions

here is an example :

<?php

define('ANIMALS', [
    'dog',
    'cat',
    'bird'
]);

echo ANIMALS[1]; // outputs "cat"

define('MYCONST', [
    'key' => "value"

    ]);

echo MYCONST['key']; // outputs "value"
about 4 years ago · Santiago Trujillo Report

0

1. If you are using PHP7 , you can define a constant as an array.

define('MYCONST', array("someValue1","someValue2"));

2. Version below PHP7 , you can store a string as a constant, whether it can be JSON string or serialized string.

define('MYCONST', json_encode(array("someValue1","someValue2")));
define('MYCONST', serialize(array("someValue1","someValue2")));

For accessing the constant in version below PHP7 (If it is JSON or serialized string), you must json_decode or unserialize respectively.

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!