What variables are required within the PHP loop block, defined in the loop outside or inside the loop?

Source: Internet
Author: User
Title, do you define a new variable within the loop that is not each cycle?


Reply to discussion (solution)

It's hard to say, look at your needs.

It's hard to say, look at your needs.
Each time a local variable is needed in the loop, is the variable defined in the loop body or in the loop body?

This depends on your needs, if you want the variable can change the value, loop to the next time can be used, put on the outside, if only the body of each loop is valid, put inside.



You can put it outside and not put it in there.
You can put it in the outer layer.

Or that sentence, if you want to use it multiple times, and the value of the variable does not change, it is advisable to put it outside. If it is used only in a loop and the value is likely to change, put it inside.

Or that sentence, if you want to use it multiple times, and the value of the variable does not change, it is advisable to put it outside. If it is used only in a loop and the value is likely to change, put it inside.

Each use is assigned a new value.

Personal humble opinion: After the use of variables, remember to unset off, do not care about the outside.

Reference to the 6 floor xianglei1130 reply: Or that sentence, if you want to use multiple times, and the variable value does not change, it is recommended to put outside. If it is used only in a loop and the value is likely to change, put it inside.

Each use is assigned a new value.

Agree with upstairs, not unset also can. Since the variable value is not the same, then wherever you put it, just re-assign it to the variable before each use. No need to dwell on it or outside.

According to the requirements of the release, if placed in the loop body, the cycle period variables will not change, if placed in the loop body, the cycle of the variable will change

Defined outside the loop body, and then initialized to its original state each time it is used in the body of the loop

  • Related Article

    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.