{
    "href": "/post/2014/10/11/this-is-why-you-should-always-use-braces-on-conditionals/",
    "relId": "2014/10/11/this-is-why-you-should-always-use-braces-on-conditionals",
    "title": "This is why you should always use braces on conditionals",
    "author": "pmjones",
    "markup": "html",
    "tags": [
        {
            "href": "/tag/php/",
            "relId": "php",
            "title": "PHP",
            "author": null,
            "created": null,
            "updated": [],
            "markup": "markdown"
        },
        {
            "href": "/tag/programming/",
            "relId": "programming",
            "title": "Programming",
            "author": null,
            "created": null,
            "updated": [],
            "markup": "markdown"
        }
    ],
    "created": "2014-10-11 17:03:49 UTC",
    "updated": [
        "2014-10-11 17:03:49 UTC"
    ],
    "html": "<p>In issue 65 on Aura.Router, <a href=\"https://github.com/auraphp/Aura.Router/issues/65\">Optional parameters not working as expected?</a>, we can see that the problem was this piece of code from the issue reporter:</p>\n<pre>if(isset($this-&gt;route-&gt;params[\"id\"]))\n    echo \"id = \" . $this-&gt;route-&gt;params[\"id\"] . \"&lt;br&gt;&lt;br&gt;\";\nif(isset($this-&gt;route-&gt;params[\"function\"]))\n    echo \"function = \" . $this-&gt;route-&gt;params[\"function\"] . \"&lt;br&gt;&lt;br&gt;\";\nif(isset($this-&gt;route-&gt;params[\"third\"]));\n    echo \"third = \" . $this-&gt;route-&gt;params[\"third\"] . \"&lt;br&gt;&lt;br&gt;\";\n</pre>\n<p>Look at that last if statement: it ends in a semicolon. Although the line beneath it that is *intended* to be the body of the if statement, as indicated by the indentation, it is *not* the body of the if statement. It gets executed no matter the outcome of the if condition.</p>\n<p>That kind of thing is really tough to debug.</p>\n<p>Instead, if we use braces on all conditionals, regardless of how short or long the body is, we get this:</p>\n<pre>if(isset($this-&gt;route-&gt;params[\"id\"])) {\n    echo \"id = \" . $this-&gt;route-&gt;params[\"id\"] . \"&lt;br&gt;&lt;br&gt;\";\n}\n\nif(isset($this-&gt;route-&gt;params[\"function\"])) {\n    echo \"function = \" . $this-&gt;route-&gt;params[\"function\"] . \"&lt;br&gt;&lt;br&gt;\";\n}\n\nif(isset($this-&gt;route-&gt;params[\"third\"])) {\n}\n\necho \"third = \" . $this-&gt;route-&gt;params[\"third\"] . \"&lt;br&gt;&lt;br&gt;\";\n</pre>\n<p>Then it's very easy to see what's going wrong.</p>\n<p>That is one reason <a href=\"http://auraphp.com\">Aura</a> adheres to <a href=\"https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md\">PSR-2</a>; it incorporates that rule, along with many others, that makes it easier to know what to expect when reading code. Anything unexpected becomes a signal that something may be wrong.</p>\n<hr>\n<p class=\"reddit-links\">Read the Reddit discussion about this post <a href=\"https://www.reddit.com/r/PHP/comments/2iyh2f/this_is_why_you_should_always_use_braces_on/\">here</a>.</p>\n"
}
