The principle of declare an array

i am a newbie of php,maybe these questions are too stupid,but i hope any one can give me more details about it,making me understand it.
first,i want to know when i declare an array in php,what’s happen to the computer,how the memory deals with the array.

Declaring an array is easy:


$array = array();

As for what that does in memory, I have no idea. You don’t have to do your own memory-management in PHP (like in some other languages).

Oh yeah, stupid questions don’t exist :slight_smile:

An array is created as Immerse stated above and is handled in memory as a collection of variables together as an object.
You don’t really need to worry about how PHP does it as all you will need to use is the actual initialisation and usage of them.

It’s really not that important… Though PHP is built on C so I’d say that they handle the storage of arrays in a computer’s memory in similar ways? I could be wrong in this fact but just my two cents.

I imagine it gets stored in memory as a pointer and data bits. AFAIK PHP doesn’t have Garbage Collection, and as long as the data isn’t put into a $_ variable the memory is freed when the script ends. But if you’re dealing with large arrays (or anything else large, or resources) and the script is long running, you may want to consider using unset() to free up some memory when you no longer need the data.


var_dump(memory_get_usage()); // 227120 
$arr = array();
for($i=0; $i<1000; $i++) {
  $s = '';
  for($j=0; $j<64; $j++) {
    $s .= mt_rand(0,9);
  }
  $arr[] = $s;
}
var_dump(memory_get_usage()); //548552
//unset($arr);
$arr = 'small';
var_dump(memory_get_usage()); // 228360 - garbage collected??!!

what’s meaning about $_ variable in you answer ?it’s a super global variable?am i right?

i am a newbie,i can’t understand the code writen by you,could you give more details about it.

In english:

Show mem usage
Assign large array to $arr
Show mem usage
Assign small string to $arr
Show mem usage

Point being that as soon as the large array is no longer referenced, the memory is freed up.

You would be best to study another language if you want a more in depth understanding of data structures and types. PHP is intentionally abstract and easy to use in this regard.

i know the example,thank you very very much.but i still don’t know what’s your purpose or want to say about this example.
there are new things confused me,the first used memory is 227120 byte,but above the statement of var_dump(memory_get_usage()); there is no line,why the memory have a value ?what is running in the memory?

Yes, by $_ I meant superglobals http://www.php.net/manual/en/language.variables.superglobals.php

I don’t know if I’d call that garbage collection. The variable is still being referenced just that it’s being assigned a new much smaller value.

How would this work?

var_dump(memory_get_usage()); // 227120 
$arr = array();
for($i=0; $i<1000; $i++) {
  $s = '';
  for($j=0; $j<64; $j++) {
    $s .= mt_rand(0,9);
  }
  $arr[] = $s;
}
var_dump(memory_get_usage()); //548552
//unset($arr);
//$arr = 'small';
// No further reference to $arr array
for($k=0; $k<1000; $k++) {
  $s = 1;
  for($l=0; $l<64; $l++) {
    $s = 0;
  }
  $s = NULL;
}
var_dump(memory_get_usage());

A lot. All the enabled php extensions, predefined functions, all the variables like $_SERVER etc…a representation of all your code in the file, and of course the php engine itself.

@Mittineague I don’t know enough about interpreters to answer, but that’s not how I thought garbage collection worked. What if something at runtime needs $arr? It would be gone.

Mittineague, it’s not really the variable itself that’s of interest. It’s the data. The variable, in a sense, just being a label to the data’s memory address. When php knows no part of the program can possibly access a piece of data, it frees the memory the data was occupying.

AFAIK as long as there isn’t multi-threading going on and a variable isn’t in a global it only has scope for as long as that script is running anyway. For example if one file has

<?php
function not_global ()
{
$x = 'yz';
}
$a = 'this';
$b = 'is';
$c = 'a';
unset($c);
$d = 'test';
echo '<pre>';
var_dump($GLOBALS);
echo '</pre>';
?>

$a, $b, and $d would be included (along with lots of others) in the $GLOBALS array ($c and $x wouldn’t be).
If you then went to another file after the first had finished

<?php
echo '<pre>';
var_dump($GLOBALS);
echo '</pre>';
?>

$a, $b, and $d would no longer be in the $GLOBALS array.
If something at runtime needs something from another file, then it should be assigned to a CONSTANT or $_SESSION etc.

Again, memory is probably of little concern most times, and you certainly don’t want to unset() something until you’re sure you’re done with it. But if the script is lengthy and uses heavy values it might be a consideration. It depends on how important utilization of resources is while the script is running.

When you return from a function, all local variables will be destroyed and memory freed(garbage collection as I understand it), likewise when a script ends. Constants won’t live outside a script, only session or cookie data.

:d’oh: Yes, of course. Thanks for pointing that mistake out. CONSTANTS are global within the script but not between pages. Don’t want to mislead anyone.

Off Topic:

4 AM here, time for some sleep