Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problem with PHP Array

Tags:

php

Why is it not possible to do something equivalent to this in PHP:

(Array(0))[0];

This is just for sake of argument, but it seems strange it does not allow access of anonymous objects. I would have to do something like the following:

$array = Array(0);
$array[0];

Any ideas why this is the behavior of PHP?

like image 834
Jotham Avatar asked Mar 01 '23 21:03

Jotham


1 Answers

I read something somewhat detailed about this once and I regret not bookmarking it because it was quite insightful. However, it's something along the lines of

"Because the array does not exist in memory until the current statement (line) executes in full (a semicolon is reached)"

So, basically, you're only defining the array - it's not actually created and readable/accessible until the next line.

I hope this somewhat accurately sums up what I only vaguely remember reading many months ago.

like image 163
Peter Bailey Avatar answered Mar 08 '23 23:03

Peter Bailey