if ($userName = = "System") { $userName _msg= "Automatic";} else{ $userName _msg= "";}
And
$userName _msg= ""; if ($userName = = "System") { $userName _msg= "Automatic";}
I want to assign a value to the variable $username_msg, what's the difference between the two ways?
Reply to discussion (solution)
There is no difference in implementation.
But from the code quality, of course, the second kind of good.
This is only two cases of judgment, of course, the third-eye operation is good
There is actually no difference.
It is only conceptually that the second type is more consistent with the first initialization before using the variable before it is initialized.
The first kind of feeling is based on different logic, but with different values to initialize the same variable, the initialization and logic is too close together.
There is no difference in implementation.
But from the code quality, of course, the second kind of good.
This is only two cases of judgment, of course, the third-eye operation is good
++
They said everything upstairs.
First, the memory is allocated once, regardless of whether the condition is true or not
Second, when the condition is set, the memory is allocated two times, and the first allocation discards the incoming garbage collection
The first, and three mesh operations ensure that the variable exists and has a value
Second, if the code that assigns the initial value and assigns the new values is too far apart, there is no guarantee that the assigned value will exist. Very likely to cause access to non-existent variables