How does PHP handle temporary expression for memory?

5

In PHP, it is possible to iterate the elements of a array through foreach , both with the variable that contains it and 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;
}

You can also reference 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!" )

This code will generate a fatal error (which 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'm not sure if I'll be able to do this, but I'm not sure what to do. ".

Regarding this section, I have some doubts:

  • How PHP treats, in each case, memory usage, values assigned to variables and values declared directly in loops , function returns, or in passing of parameters?

For example, how would you handle the% w / o of% passed by parameter in this example?

  call_user_func_array('print_r', [$_POST, false]);
  • Does the garbage collector come into the picture in cases like the one above, or is PHP already automatically discarding just after the line where the "temporary expression" is used?
asked by anonymous 29.09.2014 / 17:53

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 display error because there is no data allocated in it ...

As for the bin, he'll take care of it himself. We do not need to instantiate the recycle bin as we do in java or, in some cases, C ++. PHP will handle this on the Side-Server, and we can do nothing about it.

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

In your loop question, I think it's pretty clear, right? PHP will create temporary allocations while reading loop , after that, if you do not declare new array , it will be freed for re-query.

In your doubt call_user_func_array , although you have never needed to use it, it seems that it will give a fatal error, see that print_r does not generate array, but rather prints it, ok?

    
30.09.2014 / 14:37
0

As the manual is php for :

for (expr1; expr2; expr3)
statement

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

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

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

Another curiosity is php if you consider expression-oriented php expression

    
30.09.2014 / 14:22