Questions about assignment and judgment statements

Source: Internet
Author: User
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.