# Constant arrays

- Thread: https://externals.io/message/56949
- Original archive: https://news-web.php.net/php.internals/56949
- Started by Barbu Paul Gh. on 2011-12-19T20:20:09+00:00
- Messages: 10
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #56949 — Barbu Paul Gh. — 2011-12-19T20:20:09+00:00

URL: https://externals.io/message/56949#56949

Hello!

I wonder why I cannot assign arrays to constants.
According to the PHP manual:
"only scalar and null values are allowed. Scalar values are integer, float, 
string or boolean values."

What is the mechanism behind this?
What's the explanation?

-- 

Common sense is not so common - Voltaire
http://tweak-it.tk/ - Personal portfolio and web-log - Barbu Paul - Gh.
Visit My GitHub profile to see my open-source projects - https://github.com/paullik

---

## #56950 — Paul Dragoonis — 2011-12-19T20:23:59+00:00

URL: https://externals.io/message/56949#56950 · In reply to: #56949

Barbu,

This is how constants work in all viable languages such as C/++.

They are not 'variables of data', they contain scalar values so that
you can have a maintainable source for your value to refer to later.

http://msdn.microsoft.com/en-us/library/357syhfh(v=vs.80).aspx

I believe constants come from the philosophy of 'enums' in C.

Just google some more on it.

Regards,
- Paul.



> Hello!
>
> I wonder why I cannot assign arrays to constants.
> According to the PHP manual:
> "only scalar and null values are allowed. Scalar values are integer, float,
> string or boolean values."
>
> What is the mechanism behind this?
> What's the explanation?
>
> --
>
> Common sense is not so common - Voltaire
> http://tweak-it.tk/ - Personal portfolio and web-log - Barbu Paul - Gh.
> Visit My GitHub profile to see my open-source projects -
> https://github.com/paullik

---

## #56951 — keisial@gmail.com — 2011-12-19T20:47:43+00:00

URL: https://externals.io/message/56949#56951 · In reply to: #56950

> Barbu,
>
> This is how constants work in all viable languages such as C/++.
I disagree. In C you can have:
const data foo[] = { { "Data1", 2 }, { "Data2", 78 } };

It's not unusual in php to have a complex structure that won't change
in a variable. It should be an array, but as array is not allowed as
content,
you need to leave it as a static variable that you won't never change.

---

## #56952 — Paul Dragoonis — 2011-12-19T20:50:31+00:00

URL: https://externals.io/message/56949#56952 · In reply to: #56951

2011/12/19 Ángel González <keisial@gmail.com>:
> 
> > […]
> I disagree. In C you can have:
> const data foo[] = { { "Data1", 2 }, { "Data2", 78 } };

Agreed, i was more thinking of #define (not const), when setting up
values such as 'MAX_LEN', being a typical use case for PHP.

>
> It's not unusual in php to have a complex structure that won't change
> in a variable. It should be an array, but as array is not allowed as
> content,
> you need to leave it as a static variable that you won't never change.

---

## #56957 — Qiang Guo — 2011-12-20T00:28:51+00:00

URL: https://externals.io/message/56949#56957 · In reply to: #56949

Yes, I am wondering too. For mechanism you can refer to the source code,
and can someone explain why we cannot have constant arrays ?

Qiang

---

## #56955 — Daniel Convissor — 2011-12-20T00:43:51+00:00

URL: https://externals.io/message/56949#56955 · In reply to: #56949

Hi Barbu:

> I wonder why I cannot assign arrays to constants.

Do keep in mind that you can `serialize()` the array before storing it in
a constant.  Then use unserialize(CONSTANT) when you need the data.

--Dan

-- 
 T H E   A N A L Y S I S   A N D   S O L U T I O N S   C O M P A N Y
            data intensive web and database programming
                http://www.AnalysisAndSolutions.com/
 4015 7th Ave #4, Brooklyn NY 11232  v: 718-854-0335 f: 718-854-0409

---

## #56956 — Tom Boutell — 2011-12-20T01:02:28+00:00

URL: https://externals.io/message/56949#56956 · In reply to: #56955

This must be some strange definition of "constant" I do not understand, then (:

On Mon, Dec 19, 2011 at 7:43 PM, Daniel Convissor
<danielc@analysisandsolutions.com> wrote:
> Hi Barbu:
>
> > […]
>
> Do keep in mind that you can `serialize()` the array before storing it in
> a constant.  Then use unserialize(CONSTANT) when you need the data.
>
> --Dan
>
> --
>  T H E   A N A L Y S I S   A N D   S O L U T I O N S   C O M P A N Y
>            data intensive web and database programming
>                http://www.AnalysisAndSolutions.com/
>  4015 7th Ave #4, Brooklyn NY 11232  v: 718-854-0335 f: 718-854-0409
>
> --
> 
> 
>



-- 
Tom Boutell
P'unk Avenue
215 755 1330
punkave.com
window.punkave.com

---

## #56966 — jpauli — 2011-12-20T16:37:46+00:00

URL: https://externals.io/message/56949#56966 · In reply to: #56957

I guess constant array would mean that all the variables inside the array
dimensions should not change.
Just saying that, is a nonsense.

If a constant could be an array, then that array could not contain
variables, if not it wouldn't be constant any more as a change to one
variable inside it would change its own meaning value.

Julien.P



>
> Yes, I am wondering too. For mechanism you can refer to the source code,
> and can someone explain why we cannot have constant arrays ?
>
> Qiang

---

## #56968 — David Muir — 2011-12-20T22:43:36+00:00

URL: https://externals.io/message/56949#56968 · In reply to: #56966

> I guess constant array would mean that all the variables inside the array
> dimensions should not change.
> Just saying that, is a nonsense.
>
> If a constant could be an array, then that array could not contain
> variables, if not it wouldn't be constant any more as a change to one
> variable inside it would change its own meaning value.
>
> Julien.P
>
>

A constant can only contain static values. There was no mention of
variables.

Isn't a "constant" array basically the same as an Enum?

David

---

## #56976 — jpauli — 2011-12-21T09:01:57+00:00

URL: https://externals.io/message/56949#56976 · In reply to: #56968

Okay, but those effectively don't exist in PHP ;)

Julien



> 
> > […]
>
> A constant can only contain static values. There was no mention of
> variables.
>
> Isn't a "constant" array basically the same as an Enum?
>
> David
