Possible Duplicate:
Is it possible to declare an array as constant
Is it possible to use an array as a class constant in PHP?
I.e
const MYARRAY = array('123', '234');
If not why?
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.
Yes, You can define an array as constant. From PHP 5.6 onwards, it is possible to define a constant as a scalar expression, and it is also possible to define an array constant. It is possible to define constants as a resource, but it should be avoided, as it can cause unexpected results.
No, you cannot redefine a constant (except with runkit_constant_redefine), that's why is called CONSTANT.
A constant is an identifier (name) for a simple value. The value cannot be changed during the script. A valid constant name starts with a letter or underscore (no $ sign before the constant name). Note: Unlike variables, constants are automatically global across the entire script.
No, you can't.
But you could declare it as a static property.
public static $MYARRAY = array('123', '234');
---------------Update-----------------------------
Array const is available from PHP 5.6.
php.net/manual/en/migration56.new-features.php
UPDATE:
This is now available in PHP 5.6 https://php.net/manual/en/migration56.new-features.php
No you can't assign an Array to PHP constant.
In http://www.php.net/manual/en/language.constants.syntax.php
Constants may only evaluate to scalar values
This is the reason.
Scalar values for examples are int
, float
, string
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