Questions about assignment and judgment statements & nbsp; if & nbsp; ($ userName = "system") {& nbsp; $ userName_msg = "auto"; & nbsp;} else {& nbsp; $ u questions about assignment and judgment statements
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. Is there any difference between the two statements above? Share:
------ Solution --------------------
There is no difference in implementation.
But from the perspective of code quality, it is certainly the second kind of good.
There are only two cases for this determination. of course, it's better to perform the three-object operation.
------ Solution --------------------
Actually, there is no difference.
However, in terms of the concept that variables must be initialized before they are used, it must be the second method that is more suitable for initialization before use.
The first way is to initialize the same variable with different values based on different logic, so that the initialization and logic are too closely combined.
------ Solution --------------------
I said everything upstairs.
First, no matter whether the conditions are true or not, the memory is allocated once.
Second, when the condition is set, the memory is allocated twice. the first allocation is discarded and enters garbage collection.
------ Solution --------------------
First, and the three-object operation can ensure that the variable exists and has a value.
Second, if the code for assigning an initial value and assigning a new value is too far away, the initial value cannot be guaranteed. It is very likely that the access to non-existent variables