There is little difference and in PHP the concern should not be this. This is called micro-optimization and should be avoided until it proves absolutely necessary. If you really needed to optimize memory and speed the first thing I would tell you is to switch languages. PHP is not suitable for applications that require the best optimization.
In PHP reasonable optimizations are those in which you choose the right data structure and algorithm. Do not improve small code details.
That said, the second seems to be slightly better in performance and perhaps minimally in memory (again, reinforcement that makes no real difference). It seems to me to be more readable too (not everyone would agree). This is a good concern.
The third (which later came in edition) seems to be the slowest (I can not guarantee and will not waste time trying to figure it out, it just is not worth it) but it may not be. It is the shortest and simplest, most abstract. I would probably go into it after I've learned to use the loop well. Maybe by changing the range
by the string literal of the second.
If you can not tell the difference between them, it makes no difference. And if you measure and see that one is better than another, just change a little and the result may be different. There are many variables influencing performance making it difficult to make a definitive assessment.
Give names of meaningful variables. You tried to give good names but did not hit the target. Using rand_id
as a variable to count the loop causes confusion because it indicates that it should be something random. It seems to me that all these prefixes rand
in the names are all exaggerated because none of them holds random things. But it's just an opinion. I do not know what your real intention is.
Also avoid keeping the end key of the loop on the last line. It is easier to identify the end when she is on a line by herself. It's taste but this is something few programmers disagree with.