Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PSR-2 if-statement — what is allowed?

Tags:

Can I use if-statement like:

if(true) return $value; 

Or must use always with braces:

if(true) {     return $value; } 
like image 464
Max Lipsky Avatar asked Aug 08 '15 19:08

Max Lipsky


People also ask

Is PSR 2 deprecated?

Deprecated - As of 2019-08-10 PSR-2 has been marked as deprecated. PSR-12 is now recommended as an alternative.

What is PSR code?

The PHP Standard Recommendation (PSR) is a PHP specification published by the PHP Framework Interoperability Group (PHP-FIG). It serves the standardization of programming concepts in PHP. The aim is to enable interoperability of components. The PHP-FIG is formed by several PHP frameworks founders.


1 Answers

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.

like image 156
Mureinik Avatar answered Oct 18 '22 19:10

Mureinik