What is flow control statement in php

2.5.3. while

The simplest form of loop is the while statement:

while [expression] 
  statement

If the expression evaluates to true, the statement is executed and then the expression is reevaluated [if it is true, the body of the loop is executed, and so on]. The loop exits when the expression evaluates to false.

As an example, here's some code that adds the whole numbers from 1 to 10:

$total = 0;
$i = 1;
while [$i 

10 is not divisible by 3
20 is not divisible by 3
30 is divisible by 3
40 is not divisible by 3
50 is not divisible by 3
60 is divisible by 3
70 is not divisible by 3
80 is not divisible by 3

Smashing! That was awesome stuff.

5. do while

You could rewrite the prior example with a do while loop. The difference with a do while loop is that since the while condition comes after the actual statement code, the loop will always run at least a minimum of one time no matter what. With the standard while loop, there is a chance that the loop will never run at all, since if the expression is false, the loop won’t run! You’ll have to explore your use case, but it’s safe to say that do while loops are far less common than while and the other types of loops. This is how to write one if you like though.

6. for

The for loop is really the workhorse of looping constructs. It’s a little more clean than the while loop since it builds the counter right into the expression. No need to have an iterator in the statement body of the loop with this scenario. We can rewrite our little counting program with a for loop just like this. Note that the filtering and break mechanisms work just as well in for loops as they did with while loops. In fact, let’s do the same program but count by 100’s this time and break the loop at 700. Observe.


100 is not divisible by 3
200 is not divisible by 3
300 is divisible by 3
400 is not divisible by 3
500 is not divisible by 3
600 is divisible by 3
700 is not divisible by 3

Oh man, that is Cool and The Gang!

7. foreach

We talked a lot about arrays in this PHP Tutorial Series. We even came up with an Epic PHP Array Functions List to keep as a reference. The time has come now, for us to iterate over arrays. We will do this with the always useful foreach construct. Sometimes you’ll just want the values, and other times you might like to get the keys and the values. There are also a couple of ways to create the loop, so in total you have four options for the syntax.

foreach [ $array as $value ] {
    // do stuff to the value
}

foreach [ $array as $value ]:
    // do stuff to the value
endforeach;


foreach [ $array as $key => $value ] {
    // do stuff to the key and value
}

foreach [ $array as $key => $value ]:
    // do stuff to the key and value
endforeach;

Now let’s put our foreach skills into action. Let’s say we have an array of stocks and we want to process each one of them in turn. We’ll take an array of ticker symbols that are in all lowercase. We’ll then place a function inside the loop of our foreach construct, and each time the loop runs, and new lowercase ticker symbol will be fed into our strtoupper function. We’ll simply echo this process out to the browser to see it in action.

MSFT
GOOG
AAPL
ADBE
CSCO
JNPR
GPRO
NFLX

Now we want to be able to process not only the ticker symbol, but the name of the company each ticker symbol is associated with. We can do this using the key value pair syntax of our foreach loop. Check it out.

 

The stock ticker for Microsoft is MSFT
The stock ticker for Google is GOOG
The stock ticker for Apple is AAPL
The stock ticker for Adobe is ADBE
The stock ticker for Cisco is CSCO
The stock ticker for Juniper is JNPR
The stock ticker for Go Pro is GPRO
The stock ticker for Netflix is NFLX

So you can see that by using the foreach construct we can quickly get access to indices, keys, and values in both standard index based arrays as well as associative arrays.

8. try catch

We all know that computers and the software that run them are 100% fail proof, error free, and work perfectly at all times. If you believe that last sentence, there is a bridge in Brooklyn you may consider buying 🙂 Actually, the try catch construct is available to us for the very fact that sometimes things can and will go wrong with our software. The try catch gives us the ability to try something out, and if things don’t go as planned we have a contingency plan to handle any problems. A perfect example is connecting to a database. There are several things that could go wrong. To deal with this, just put your code in a try catch block like so.

 

We are ready to work with the database!

If something went wrong, like the database not being found, we get an error.

 

Whoops! That was an error: SQLSTATE[HY000] [1049] Unknown database ‘verge’

Script ended unexpectedly.

9. return

The return statement is used so much we don’t even think of it many times. It just exists happily in our code, returning values or control to the calling code going about it’s business. Usually we’ll see a return control statement being used in a function something like this.

 

12

In this code we create a user defined function that returns a value. You can see when we call that function by simply writing it’s name and passing in two variables, it assigns or returns the sum into the variable $result. We are then free to echo out that value to the screen. Pretty Slick!

What is a flow control statement?

The script interpreter processes statements sequentially starting with the first statement of the program block. This sequential flow can be controlled with conditional statements that perform branching and iteration.

What is control flow statement explain with example?

Many programming languages have what are called control flow statements, which determine what section of code is run in a program at any time. An example of a control flow statement is an if/else statement, shown in the following JavaScript example. var x = 1; if [x === 1] {

What are the 3 types of control structures in PHP?

PHP supports a number of different control structures:.
elseif..
switch..
while..
do-while..
foreach..

What are the 3 types of control flow?

Flow of control through any given function is implemented with three basic types of control structures:.
Sequential: default mode. ... .
Selection: used for decisions, branching -- choosing between 2 or more alternative paths. ... .
Repetition: used for looping, i.e. repeating a piece of code multiple times in a row..

Chủ Đề