Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to declare an array as constant [duplicate]

Tags:

php

We can define a constant like

define("aconstant','avalue'); 

Can't we define array in this fashion like below ?

define("months",array("January", "February", ---);  
like image 416
mrN Avatar asked Sep 27 '10 11:09

mrN


People also ask

Can an array be declared as a constant?

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.

Can an array be a class constant?

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.

How do you declare a constant in Golang?

To declare a constant and give it a name, the const keyword is used. Constants cannot be declared using the := syntax.


1 Answers

you can use const for that purpose since PHP 5.6 (via nikic).

const months = ["January", "February"]; var_dump("January" === months[0]); 
like image 53
masakielastic Avatar answered Sep 22 '22 11:09

masakielastic