Consider:
if (a = 10)
{
/* do something */
}
I am interested if it is a good thing to do or not and why as I have to face it quite often. Thank you in advance.
Please consider a more complex example. What would you prefer and why?
function getSomeData()
{
return rand(0, 5);
}
First:
if ($result = getSomeData())
{
print($result)
}
or Second:
$result = getSomeData();
if ($result)
{
print($result);
}
In this particular example, the
ifis unnecessary, the return of the affectation is always 10.The following would be more interesting:
In this case, I prefer surrounding it with another pair of parentheses, in order to clarify your intention to other coders:
A good rule to remember is that if you have to think more than 10 seconds to understand a condition, maybe there is another way to express it.