Want to start off by apologizing if this has already been asked; wasn't sure what exactly to search for (did a quick search but didn't find anything).
I have the following basic PHP code (this is just a simple example of what I'm doing anyways):
$TestVarOne = 'Test One';
if ( $TestVarOne != '' ) {
$TempVarOne = 'Hey You ';
$TestVarOne = $TempVarOne . $TestVarOne;
}
What I'm wondering: do I need to bother putting/amending an "else" statement?
What I basically mean is; should I write it this way/add to the code:
$TestVarOne = 'Test One';
if ( $TestVarOne != '' ) {
$TempVarOne = 'Hey You ';
$TestVarOne = $TempVarOne . $TestVarOne;
} else {
$TestVarOne = $TestVarOne;
}
Thanks for any help.
To put it simply: no.
Generally speaking, we should strive for the most readable version of our code (and most efficient). Adding an else statement that does nothing will complicate your code unnecessarily.
No. Remember: else
is not always necessary.
i think it is not necessary to use else Please look at export:
$TestVarOne = 'Test One';
if ( $TestVarOne != '' ) {
$TempVarOne = 'Hey You ';
echo $TestVarOne = $TempVarOne . $TestVarOne; //Export: Hey You Test One
}