What is process control and process control?

Source: Internet
Author: User
Tags php basics

What is process control and process control?
Analysis of PHP Process Control

Today, I will share my PHP basics with Tom on the Qi number (mutual consultation ~~) :

The following are the information collected by the Qi number and their summary hope to help:

What is process control: In Declarative Programming Languages, process control commands refer to commands that change the sequence of running programs, which may be commands running at different locations, or in two (or multiple segments) segments) select a running program.

Imperative programming: The command "machine" is used to do things (how). No matter what you want (what), it will follow your command.
Declarative Programming: Tell the "machine" what you want and let the machine figure out how to do it ).

Wait for two more programming pants.

Whether it is PHP or other syntaxes, a program is always composed of several statements.

From the execution method, the statement control structure is divided into the following three types:

1. Ordered Structure: From the first statement to the last statement, it is executed in full order;

2. Select structure: Execute several tasks based on the intermediate results of user input or statements;

3. Loop Structure: execute a task multiple times based on a condition, or wait until the target is reached.

 

There are three control statements in PHP to implement the selection structure and loop structure:

1. Conditional Control statements: if, else, elseif, and switch;

2. Loop Control statements: foreach, while, do... while... and;

3. Transfer control statements: break, continue, and return.

Below are a few small instances for Reference

Conditional Control statement:

 

If statement, usage:

 

If (E)

Statement Block 1;

Else

Statement Block 2;

 

Resolution: If the return value of E is true, statement Block 1 is executed; otherwise, statement Block 2 is executed.

 

Example code:

 

<? Php

$ A = 59; // determine whether to ask for a sister based on the value of $. If the value is greater than or equal to 60, code is output.

If ($ a >=60 ){

Echo "sister-in-law ";

} Else

Echo "Code required ";

?>

 

If · elseif · else statement, usage:

If (E)

Echo 'Sister-to-sister ';;

Else if (X)

Echo 'Don't sister ';;

Else

Echo 'Code required ';

 

Resolution: If E is true, execute it. Otherwise, if the value of B is true, do not be a sister; otherwise, the execution language requires code. Of course, if statements can also be nested.

 

The following is an example of If · elseif · else:

 

<? Php

$ A = 59;

If ($ a> = 60) // when the value is greater than or equal to 60

{

If ($ a = 100)

Echo "sister-in-law ";

Elseif ($ a> = 90)

Echo "sleeping girl ";

Else

Echo "cannot sleep ";

}

Else

Echo "Sleep Street ";

?>

 

Switch statement, Syntax:

 

Switch (E)

{

Case val1:

Statement Block 1;

Break;

Case val2:

Statement Block 2;

Break;

Default:

Statement block 3;

}

 

When the value in a case statement matches the value of switch expression E, PHP starts to execute the statement until the switch program segment ends or the first break statement is encountered.

(If break is not encountered, PHP will continue to execute the next case ).

Break is the end of the entire loop, and continue is the end of the word Loop

 

The following is an example without a break:

 

<? Php

Switch ($ leve1)

{

Case 3:

Echo "advanced ";

Case 2:

Echo "intermediate ";

Case 1:

Echo "elementary ";

Default:

Echo "wrong grade value ";

}

?>

The execution result is: grade value of the advanced intermediate primary error.

So what do you think ??

 

<? Php

$ Level = 3;

Switch ($ level)

{

Case 3:

Echo "grant administrator permissions ";

Case 2:

Echo "grant Website Service Permissions ";

Case 1:

Echo "grant moderator permissions ";

Default:

Echo "grant common user permissions ";

}

?>

 

Compared with if, switch achieves higher efficiency:

 

<? Php

$ A = 59;

Switch ($)

{

Case $ a = 100;

Echo "full score ";

Break;

Case $ a> = 90;

Echo "excellent ";

Break;

Case $ a> = 60;

Echo "pass ";

Break;

Default:

Echo "fail ";

}

?>

 

So what is the use of loop statements? Of course, it is used to execute an Operation repeatedly.

 

While and do... while

 

While Syntax:

 

While (E)

Statement block;

 

Resolution: Execute statement as long as E in the while expression is TRUE.

Do... while Syntax:

Do

{

Statement block;

}

While (E)

 

The difference between do... while and while is that at the end of the loop, do... while checks whether or not the loop conditions are met.

 

For example:

 

<? Php

$ A = 5; // first judge whether $ a is greater than 5. If it is greater than 5, execute.

While ($ a> 5)

{

Echo "This is while .";

$ -;

}

Do // first execute the statements in do and then judge.

{

Echo "This is do... While .";

$ -;

}

While ($ a> 5)

?>

 

For statement, Syntax:

For (A; B; C)

Statement;

 

Analysis: The first expression is executed unconditionally at the beginning of the loop. Generally, expression A is A value assignment statement, and expression B is run before the loop starts. If the expression is TRUE,

Then, the loop is continued and the nested statement of the loop is executed. C is executed after the loop, which is generally the auto-increment and auto-increment operations.

Code:

<? Php

For ($ a = 5; $ a> 5; $ -);

Echo "This is ";

?>

 

The Foreach statement is used for Array traversal and will be learned later.

 

Transfer control statement

 

PHP mainly has three transfer control statements: break, continue, and return.

 

1. break statement

 

The break statement is used to end the current loop. break can accept an optional numeric parameter to determine the number of loops to exit.

 

Example:

<? Php

$ A = 5;

$ B = 10;

While ($ a <100) // $ a <100 start Loop

{

Echo "a =". $ a. "<BR>"; // The concatenation operator when $ a, "." is output, which is equivalent to "+" in java"

While ($ B> 0) // $ B> 0 to start the loop

{

Echo "B =". $ B. "<BR>"; // output $ B

$ B -;

If ($ B = 3) // if $ B = 3, The while ($ B> 0)

Break;

}

$ A ++;

If ($ a = 30)

Break; // If $ a = 30, the while ($ a <100)

}

?>

 

Continue statement

Continue is used to jump out of this loop. Unlike break, the next loop will continue after the Continue jumps out.

 

The Return statement is used to end a function or script file. If you call the return statement in a function, the execution of the function is immediately ended and its value is returned as a parameter.

 

Of course, return can also be used as a function in PHP. For example, return (), and write the parameters to be returned in parentheses. This usage is not common.

The following figure is provided for your consideration.



Well, I hope you will give me more advice if you haven't mentioned any Qi number.

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.