Suddenly it became interesting - are there any differences in the speed of the php script:

if(1!=1){ echo "Ошибка"; exit; } /* дальше много - много кода */ 

or

 if(1==1) { /* много - много кода */ } else { echo "Ошибка"; } 
  • 1. If you do not know what will be faster, take and measure. 2. Do not engage in premature optimization. 3. This is not a place in the code that needs to be optimized. - Athari
  • @Athari, man has become interested , you know? He is trying to grasp the essence, to rise above the average gray level. And you to him ... (and the answers-advice as from a person who does not know the answer, but wants to say something). (and a plus to this is also minus a mercilessly newbie, not good). - avp
  • @avp, interesting is something less ... immeasurable :) - user31688
  • one
    @avp This is a disgusting question that leads to writing disgusting answers. It is better for a newcomer to immediately understand that he is engaged in nonsense than to go into detailed explanations around the initially incorrect question. / My first comment is the most correct answer in this situation. But attempts to arrange benchmarks - to a lesser extent. - Athari
  • one
    @avp, @ Athari, Girls, don't quarrel, you're both beautiful. And both are right ... from some side. From the first - this is a bad question and you should immediately make it clear that the author does not suffer from garbage, everyone (and the author) will be better off. On the other hand, understanding how the interpreter works, its hints and peculiarities are great, there is a chance to grow above yourself and others :) - user31688

3 answers 3

If you can not measure, you just need to think.

If the function code is first read entirely before execution and compiled into some form for the interpreter, there will be no difference in the execution time.

And if you interpret the code "in the forehead" ie immediately as you read it, the first form

 if (condition) {... exit;} 

no doubt it will work faster (if the condition in if == true).

So the question comes down to - "How does the PHP interpreter work?"

-

My guess is - there will be no time difference.

-

One more note - you should not do measurements when in if constant expression.

Update

I see in the comments the lack of understanding of the text of the answer.

In the summary, I wrote - "My guess is - there will be no time difference."

This means that some kind of preliminary compilation of the function body is carried out, at least all its text will be read before execution, i.e. in reality, the code is not executed while reading.

For confirmation - a small example

 avp@avp-xub11:hashcode$ cat t.php <?php if(1!=1) { echo "1\n" echo Ошибка1; } else { echo "2\n"; echo Ошибка2; } ?> avp@avp-xub11:hashcode$ php <t.php PHP Parse error: syntax error, unexpected 'echo' (T_ECHO), expecting ',' or ';' in - on line 5 avp@avp-xub11:hashcode$ 

As you can see - Parse error in a block that is not executed. But the error response echo Ошибка2; we will see if we fix the first (missing ; )

 avp@avp-xub11:hashcode$ php <t.php 2 PHP Notice: Use of undefined constant Ошибка2 - assumed 'Ошибка2' in - on line 10 Ошибка2avp@avp-xub11:hashcode$ 

It seems to me that now everything is quite obvious.

  • As far as I know, PHP uses compilation on the fly, so with line-by-line execution an easy bummer. - user31688
  • @TheDoctor Everything is worse: depends on the version, settings, etc. - Athari
  • @Athari, There is such a thing, it was too lazy to paint, and I don’t know exactly what the puff has under her skirt, i.e. the hood. - user31688
  • Added some additional clarification. - avp

And nothing that this is actually different designs and you can not compare them?

  • And nothing that the result of the code is the same, but the speed can theoretically be different? So you can compare and need. In essence, it wonders if php will even look at what is after the exit ... - Vasily
  • one
    @ Vasily, Dude, these are language constructs, not functions (yes, exit is a construct, proof ), they cannot be measured (perhaps, of course, everything, but there it’s necessary to disassemble the core of the puff, who needs it?). In any case, it will look like an exit - collect classes, functions, constants, etc. But it will not perform. - user31688
  • first worthy answer! In the first case, php will be chopped off right away, in the second case it will look for if terminations to go to the execution of else, is it? - Vasily
  • @ Vasily, You 're welcome :) - user31688
  • That's the question that he will do faster, be cut off immediately, or be cut off after the entire script is executed - Vasily

On the first link in google:

 $start = microtime(true); // ВАШ СКРИПТ $time = microtime(true) - $start; printf('Скрипт выполнялся %.4F сек.', $time); 
  • 2
    I will sadden you: for the benchmark if resolution of microtime not enough. (Or I’ll be completely disappointed in PHP.) - Athari
  • @Athari, Well, you can try to make stopitsot cycles on if :) - user31688
  • one
    Well, the fact of the main question - "are there any differences in the speed of the php script execution" reflected the essence of the verification method, and then let it turn on the brain. In any case, if there is not enough, as the “benchmark” put it, then they will see that the implementation takes place too quickly, according to indicators. - IvanZakirov
  • @TheDoctor Great. How to make stopitsot exit calls? - Athari
  • @Athari is precisely why he is taller and replied that in no case should they be compared - MaboUhha