Multiple nesting of if statements in PHP flow control

Classmate Wang Sixong We told in the first story that he has two secretaries: a life secretary and a work secretary.

Wang Sixong is also extremely well-planned in his travels and projects. He assigned business trips to his life secretary and work secretary respectively:

In life:
Check the weather first, bring rain gear and towel if it rains. Wear sunscreen if it’s not raining
The condition of rain gear, towels and sunscreen should be checked in advance. If not, buy them immediately

At work:
It is necessary to communicate in advance about the work plan before going to Dalian. When ready, it is necessary to check in time, check if it is qualified, and print and sign the form.
If not prepared in time, the main project communication topics should be listed.

Similar to the above situation, we need to use the if...elseif...else repeatedly nested structure.

One or more if statements can be nested in the if statement to realize the judgment of multiple parameters. This is the multiple nesting of if statements. Its structural form is as follows:

We use a flow chart to express it as follows:
2015-08-08/55c5a2ff40df7

Note:

  1. We are in code segments 1 and 2 , 3 and 4 can be added to the judgment. According to the actual situation, you can also add nesting
  2. Pay attention to the indentation. The function of indentation is only to make the code rich in layering, beautiful and easy to read, and has no impact on the generation of the target code.

We can use codes to express the life requirements of Mr. Wang Sixong in a nested structure. We used a three-level nested structure, and the code is as follows:


        

Warning: For novice programmers, please use caution when using this nested if...else loop. Because too many layers of loops can easily cause problems in the design logic, or there are too few braces, etc., which will lead to inexplicable problems in the program.

I hope you can write it out silently. Also, there cannot be a single grammatical error. In the future, we can use it at any time. If we want to react immediately in the brain, we can start writing.

Continuing Learning
||
submit Reset Code
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!