Reduction if then still performance / optimization in php

Speaking of PHP, I would like to ask if there is a performance difference between the two:

$name=($IsBoy)?"George":"Mary"; 

vs

 if($IsBoy) { $name="George"; } else { $name="Mary"; } 
  • Will these two results work with a different opcode?

  • If so, will there be any theoretical performance difference? (of course, ignore the time when these two should be read / compiled / interpreted)

  • If yes, then optimizers, such as the zend optimizer, will take advantage of this and do any re-layouts automatically?

ps if you think my code for "full-longhand" if-then-else is too complicated, provide an example of the most basic code and the answer to it.

UPDATE:

I was hoping the question was perfectly clear, but it seems people are not getting the message. This question relates to a THEORETICAL (... but real and mesaureable) performance difference (which is why I applied bold and italics to a theoretical one ). Please do not answer by saying that the programming style is more readable and that it is too annoying to worry about performance.

ps 2: paying special attention to the theoretical word, I try to prevent answers like "don’t worry that it’s not worth the trouble, its just nanoseconds."

+4
source share
3 answers

Ok that's pretty interesting

I quickly checked x debug for your towing examples accordingly

and that's what i got

enter image description here

enter image description here

I am sure that you can have a different result, with each update they all guarantee that

The second method is always better than the first

Although with each update you may find the first method, from time to time, but this is due to optimization of the PHP core.

regarding the Zend optimizer, I have not tested that

+4
source

I often throw away if-then-else in favor of:

 $name = 'the expected'; if ([expr]) $name = 'the exception'; 

Easy to read, less braces and compact.

0
source

Both the normal ifelse operator and the triple operator have a slight performance difference. You can check the stackoverflow links below for an additional link

Press here

-1
source

Source: https://habr.com/ru/post/1489098/


All Articles