Questions about assignment and judgment statements

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

  • 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.