24

Can I use if-statement like:

if(true) return $value;

Or must use always with braces:

if(true) {
    return $value;
}
Mureinik
  • 297,002
  • 52
  • 306
  • 350
Max Lipsky
  • 1,774
  • 1
  • 18
  • 29

1 Answers1

24

Section 5.1 of the psr-2 standard explicitly states that:

An if structure looks like the following. Note the placement of parentheses, spaces, and braces; and that else and elseif are on the same line as the closing brace from the earlier body.

<?php
if ($expr1) {
    // if body
} elseif ($expr2) {
    // elseif body
} else {
    // else body;
}

So, according to psr-2, you must use braces for an if statement.

Mureinik
  • 297,002
  • 52
  • 306
  • 350
  • 7
    Thanks! I found this in 5 section (one line before): `The body of each structure MUST be enclosed by braces. This standardizes how the structures look, and reduces the likelihood of introducing errors as new lines get added to the body.` I do not know how I missed it. – Max Lipsky Aug 09 '15 at 08:53