We can define a constant like
define("aconstant','avalue');
Can't we define array in this fashion like below ?
define("months",array("January", "February", ---);
If you want to make an array constant, you can precede its type with the word const. When you do so, the array name is constant, and the elements inside the array are also constant. Thus you cannot have a constant array with nonconstant elements, nor can you have a nonconstant array with constant elements.
No, you can't. But you could declare it as a static property. Array const is available from PHP 5.6. @Martin Yep, that's true, but you can not declare a constant with an array.
To declare a constant and give it a name, the const keyword is used. Constants cannot be declared using the := syntax.
you can use const for that purpose since PHP 5.6 (via nikic).
const months = ["January", "February"]; var_dump("January" === months[0]);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With