Ternary operators and variable reassignment in PHP

后端 未结 4 1228
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-20 08:22

I\'ve perused the questions on ternary operators vs. if/else structures, and while I understand that under normal circumstances there is no performance los

4条回答
  •  感动是毒
    2020-12-20 09:21

    The topic that using a ternary here is not optimal has already been covered above. I'm going to address your question about whether it will reassign the value:

    This depends on what you call "reassigning". PHP does not optimize, so the $foo = $foo will be evaluated. On the other hand this will not cause PHP to copy the value of $foo to a new chunk of memory. Probably PHP will just increase the refcount on $foo and then immediately decrease it (though I'm not sure about the exact implementation details of self-assignment). So, even though PHP will execute the statement, it won't affect performance (unless you choose to write $foo = $foo seven million times in your code).

提交回复
热议问题