What is the benefit of using multiple steps to test variables:
$VarLength = strlen($message);
if ($VarLength > 10)
echo "Over Ten";
…versus just pushing the whole process into one if statement:
if ( strlen($message) > 10 )
echo "Over Ten";
I’m wondering if the benefits go beyond code style, and the ability to re-use the results of the (in the example above) strlen result.
If the result of a function will be used multiple times, it should be cached in a variable so as to obviate the need to waste resources to re-calculate its result.
If the function result won’t be re-used, it can simply be a matter of code readability to clearly delineate what’s happening by storing the function return value in a variable before using it in an
ifcondition.Also, in terms of readability, you should always use curly braces even when not mandated by PHP syntax rules as @AlexHowansky mentions.