How does PHP treat temporary expression regarding memory?

Asked

Viewed 378 times

5

In PHP, it is possible to iterate the elements of a array through the foreach, both with the variable that contains it and with what PHP called "Temporary array Expression". Example:

$myArray = ['a' => 'a', 'b' => 'b'];
foreach ($myArray as $key => $value) {
    echo $value, PHP_EOL;
}


foreach(['a' => 'a', 'b' => 'b'] as $key => $value){
    echo $value, PHP_EOL;
}

It is also possible to refer to each element of a array, and that’s where my question comes in.

The code below works correctly:

$myArray = ['a' => 'a', 'b' => 'b'];
foreach ($myArray as $key => &$value) {
   $value = sprintf('"%s!"', $value);
}
print_r($myArray); //imprime: Array ( [a] => "a!" [b] => "b!" )

Already this code will generate a fatal error (what I expected):

foreach (['a' => 'a', 'b' => 'b'] as $key => &$value) {
   $value = sprintf('"%s!"', $value);
}
//Erro:  Cannot create references to elements of a temporary array expression

I already know that trying to pass a statement by reference generates error, but my focus is not that, but rather that the error says in an excerpt "... Temporary array Expression...".

Regarding this passage, I have some doubts:

  • What is the way in which PHP deals, in each case, with memory usage, values assigned to variables and values declared directly in loops, function returns or in passing parameters?

For example, how the array passed by parameter in this example?

  call_user_func_array('print_r', [$_POST, false]);
  • The garbage collector enters the scene in cases like the above or PHP already automatically discards right after the line in which the "temporary expression is used"?
  • 2

    Related question in English (does not answer your question): http://stackoverflow.com/questions/24772958/is-there-a-rational-explanation-for-this-php-call-by-value-behavior-or-php-bug.

2 answers

1


PHP, as our friend @Heat said, considers itself Object Oriented, that is, it will create a logical instance the first time it sees the variable.

If this variable is called as echo, print, print_f, printr, PHP will show error, because there is no data allocated in it...

As for the recycle bin, it will take care of it itself. We don’t need to instantiate the recycle bin as we do in java or, in some cases, in the C++ language. PHP will handle it on Side-Server, and there’s nothing we can do about it.

I can give you some tips if you want to reuse a variable or if you have a problem with SQL. Always, I say ALWAYS SAME, close the Mysql/mysqli connection so that you can safely use it at another time; and, if you want, declare the variable null before use, this will clear its allocation and will leave it free for new entries.

In your doubt of loop, I think that’s pretty clear, right? PHP will create temporary allocations while reading the loop, after that, if you do not declare new array, she will be released for further consultation.

In their doubt call_user_func_array, although I never had to use, it seems to me that it will give fatal error, see that print_r does not generate array, but prints it, ok?

  • No, call_user_func_array works correctly in the example. including it prints Array() in this case, as it accepts two parameters print_r(mixed $data, boolean $return)

  • The example of call_user_func_array calling the print_r was to represent the use of an "array temporary expression". The print_r prints the value of array, and the call_user_func_array calls a function using an array as if it were the parameter of the same. What I want to know is how PHP would treat [$_POST, false], which has not been assigned to any variable

  • 1

    Oops, my fault, sorry. For his documentation [http://php.net/manual/en/function.call-user-funcy.php] o call_user_func_array shall first pass the instruction of echo and then the array... in that case we would have to know if the $_POST will be an array. Hugs

  • So, as I said earlier, in fact there will be no error in the hypothesis that $_POST is an array.. Otherwise you will fall into that error of passing a string/int/Boolean (anything other than the array) when an array was expected...

0

According to the manual php for:

for (expr1; expr2; expr3)
statement

It evaluates the first expression only once, whether it is a $i or an Expression array [], at each iteration it checks the boleane of expr2 and at the end of the iteration runs expr3.

Since it is inside a block, a context is created for the execution of the commands inside the for

Once the execution block is finished the variables can already be marked to be released.

Another curiosity is php considering itself oriented to expression php Expression

Browser other questions tagged

You are not signed in. Login or sign up in order to post.