Performance
I’m not an expert on the subject, but I can assure you that the performance in this case doesn’t make so much of a difference that it’s worth choosing one or the other because of it.
Safety - Understanding each other’s behavior
The only safety that is at risk is yours. I say this because it is necessary to understand the behavior of references in this case.
How it was answered in this question there is a small variation from what is expected of a normal foreach in relation to the variable passed by reference.
In short: The last value is always referenced, it can cause you problems if you overwrite the value of $value
.
$array = [1, 2, 3];
foreach ($array as $key => &$value) {
$value = $value . 'R$';
}
$value = null;
That would generate:
['1R$', '2R$', null]
See an example on IDEONE
So after using a foreach
with reference, it is always good to call unset
on the referenced variable (after the loop).
I won’t talk about it much here, because the linked question has enough on the subject.
In this case, I’m not telling you not to do it like this (I’ve done it a few times). I’m just warning you that you better know everything about it, so that "you don’t know" causes you problems.
But, noting that may arise a "new behavior" in relation to the first foreach
- and at the same time, it can also affect readability if you forget to treat the reference variable properly-, I could think of your second option as being the best.
$array = [1, 2, 3];
foreach ($array as $key => $value) {
$array[$key] = $value . 'R$';
}
But it is necessary to know if it is necessary and if it is wise to change the original value of your array
. If you’re sure you won’t be using the array
""original, so you can do it without fear of being happy :p
The reference disadvantage with foreach
Remembering that also you have a disadvantage when using foreach
with reference: You can only loop with the array
stored in variables. Arbitrary expressions cannot be used with reference in some versions of PHP prior to 5.5.
Example:
// Certo:
$array = [];
foreach ([1, 2, 3, 4] as $key => $value) {
$array[$key] = $value . 'R$';
}
// Errado: Isso vai gerar um erro caso use PHP 5.4
foreach ([1, 2, 3, 4] as $key => &$value) {
$value = $value . 'R$';
}
See a small test
Draw your own conclusion
I don’t want to give you an opinion on the subject, I just want you to understand that do a foreach with reference and do a reallocation with the keys of the foreach
may seem like the same thing, but technically it’s not. References can be villainous if not used wisely. Similarly overwrite a value of a array
might not be a good idea if you want to keep the original values.
I’m showing you some points, because this "good practice" thing is actually pretty personal. Always better you know what you’re doing and know how to use it at the right time.
Related: http://answall.com/questions/80815/behavior-do-foreach-com-vari%C3%A1veis-by-refer%C3%Aancia
– Wallace Maxters
As much as the two questions are about foreach, I don’t think they’re alike.
– Guilherme Pressutto
"Related" is not "duplicated". At no time said they were similar. Your question received votes for closure because it takes the response based on opinions.
– Wallace Maxters
@Sorry wallacemaxters, I’m new to the site
– Guilherme Pressutto
My linked question is already a point for you to avoid using by reference, or, if using, use carefully.
– Wallace Maxters
No problem, William. You can edit your question to stay within the scope and be accepted. I think the question itself aborts a very interesting topic, but the problem was the final question. She asked for an opinion. I think wanting to know the issue of safety and performance is not bad, but the issue of readability is already very personal.
– Wallace Maxters
The guy’s asking about performance and best practices, this link there has nothing to do.
– Pedro Morais
If you don’t mind, I can edit the last snippet so she stays within the scope...
– Wallace Maxters
@Pedromorais is a point for him to avoid use, as I said before. And to tell you the truth, his answer is that he didn’t answer anything. I commented below the negative reason to show that the answers need to be improved.
– Wallace Maxters
@Wallacemaxters But I asked the opinion because I want to know the same personal answer, it’s like asking if in your opinion is worth using
if
without keys. One of the answers would surely be related to good practices and would say to always use keys in theif
.– Guilherme Pressutto
"Good practice" is very relative. This is the problem. What is good for me, can be bad for you, you understand... So I’m going to try to give you as impartial an answer as possible.
– Wallace Maxters
@Guilhermepressutto I will give a +1, because although it leads to opinions, I believe that some aspects of the questions are interesting to lead to an answer not based on opinions, but that clarifies about the differences between one and the other.
– Wallace Maxters