哪个是更好的性能明智 - 如果(条件){结果} VS如果(条件)结果;

Dzh*_*eyt 0 php performance conditional curly-brackets

我知道两种情况下的性能差异都非常小,但我想知道哪种是更实用,性能更高的版本来编写PHP条件.

if(condition){
  result;
}
Run Code Online (Sandbox Code Playgroud)

VS

if(condition)
  result;
Run Code Online (Sandbox Code Playgroud)

gdo*_*ica 7

两者完全相同,它的编码风格与性能无关.适应您喜欢的风格.

一些提示工具建议使用第一种样式来避免这样的错误:

if(condition)
  result;
  foo; // Nothing to do with the condition but you can get confused.
Run Code Online (Sandbox Code Playgroud)

如果你使用花括号,这不会发生:

if(condition){
    result;
}
    foo; // Nothing to do with the condition but now it's clear.
Run Code Online (Sandbox Code Playgroud)

顺便提一下,我没有使用大括号if,因为这种情况对于非新手来说并不难避免.