Suppose I have a conditional statement without brackets in a function:
function get_user_id()
{
if ($something) $this->some_function($str);
return array('user_id' => $user_id);
}
I think bracketless statements are bad practice… I want to add brackets to the if-statement but where does the statement end? Does is it end at the first semicolon or the end of the function?
Which is correct:
function get_user_id()
{
if ($something)
{
$this->some_function($str);
}
return array('user_id' => $user_id);
}
or..
function get_user_id()
{
if ($something)
{
$this->some_function($str);
return array('user_id' => $user_id);
}
}
The first version is correct, as without braces, only one statement after the
ifstatement applies to the conditions of theifstatement.So this:
Is equivalent to:
I do agree that the manual is a bit vague about this, but its example is about a single statement, and mentions that in order to incorporate additional lines to the same conditional, you would need a “statement group”, denoted by curly braces: