tags:

views:

179

answers:

8

In this situation, is it better to use a loop or not?

echo "0";
echo "1";
echo "2";
echo "3";
echo "4";
echo "5";
echo "6";
echo "7";
echo "8";
echo "9";
echo "10";
echo "11";
echo "12";
echo "13";

or

$number = 0;

while ($number != 13)
{
    echo $number;
    $number = $number + 1;
}
+3  A: 

It's clearer to use a loop; as a rule of thumb, if you use fewer lines of code to loop, do so.

Your loop could be written more succinctly:

foreach (range(0,13) as $n)
    echo $n, "\n";
meagar
thanks, i had 600 lines of codes, when I could of used a loop and turned it all into around 200 or less.Just wanted to know if a loop loads faster.
Joseph Robidoux
If processing speed is that important to you, you should be using machine code, not PHP. Being able to reasonably work with the code is far, far more important than the miniscule performance boost here.
ceejayoz
+2  A: 

I'd use a for loop, just to keep everything out in the open (your original loop seems to only print up to 12):

for ($number = 0; $number <= 13; $number++)
{
    echo $number;
}

It's a lot cleaner then writing out a million 'echo', and the code is fairly self explanatory.

robbo
+4  A: 

The former may be a little faster. The latter is a lot more readable. Choice is yours ;)

Kornel Kisielewicz
+5  A: 

Even better would be

$totalWhatevers = 14;
for ($number = 0; $number < $totalWhatevers ; $number++)
{
    echo $number;
}

Where 'totalWhatevers' is something descriptive to tell us what you are actually counting.

Dan McGrath
A: 

Depending on what you are trying to do, a loop could help to keep your code clearer and simpler to update.

For example, when the number of display could vary, you could use a config variables.

$repeat = 20;

for ($i = 0; $ < $repeat; ++$i) {
   echo $i;
}
Boris Guéry
A: 

No, Joseph your method is bad. It is not recommended.

Use for statement.

Toktik
+1  A: 
$string = "<?php \n";

$repeatNum = 20;

for($i = 0; $i < $repeatNum; $i++)
{
    $string .= "echo \"" . $number . "\"; \n";
}

$string = "?>";

Now you can either

eval($string);

or

file_put_contents("newfile.text", $string);

and you will get a file with all the echos!

Note: This is not really a 'serious' answer. If you really want to create a PHP file with a number of echos, it works, but evaling the statement at the end is probably not the best idea for regular programming practice.

Chacha102
Why would you post a non-serious answer for a question tagged beginner? Now this is the accepted answer and it is absolutely horrible!
erisco
A: 

The mistake is in thinking that a loop generates code. This is not the case at all.

echo "0";
echo "1";
echo "2";
echo "3";

PHP will come along and execute each of these statements one by one.

for ($i = 0; $i <= 3; ++$i) {
    echo $i;
}

In this case, PHP executes the same echo statement four times. Each time the end of the block is reached (a block being code between curly braces) execution jumps back to the condition. If the condition is still true, the block executes again.

Which method offers the best performance? It is very, very hard to tell.

Slowdown of method 1) The greater the range of numbers to echo, the more echo statements that have to be parsed in the script.

Slowdown of method 2) The greater the range of numbers to echo, the more times execution has to jump, the more times a condition has to be tested, and the more times a counter has to be incremented.

I do not know which scenario leads to more CPU instructions, or which leads to more memory usage. But as someone who has programmed before, I know a secret: it doesn't matter. The difference in execution will probably weigh around a few ten millionths of a second!

Therefore, the only way to make the effects become noticeable at all is if you are echoing ten million numbers, in which case one method might take a second more than the other. Isn't this a worthy gain though? No! By the time we are echoing ten million numbers, we are going to be spending minutes on either method. Only the difference between them will differ by a second, which is completely negligible. Not to mention, who knows which one is better anyhow?

As a programmer, the best thing you can do is make your code readable and maintainable. A loop is much easier to understand than a page of echo lines. With a loop I know you haven't missed any intermediate numbers, but with echoes I have to check every single line.

Technical jargon version: both algorithms have complexity O(n). There can only be a constant difference in their performance, and if that constant is not particularly large, it is negligible.

erisco