Monday, January 8, 2018

Flow control function in PHP

Flow control statements 

PHP support on number of traditional programming constructed for controlling the  flow of execution of program.
           Conditional statements, such as if/else and switches allowed a program to execute different pieces of code on none at all depending on some condition loops such as while and for, support the repeated execution of particular segment of code. 
             Code would be quite boring if there were always only one way through it, with no alternative course and no varying outcomes. The next area of PHP that we will look at is the Decision Process that you can define using flow control statements. - The Best Popunder Adnetwork

The control statement are various names types of mention here:-
(I)  If...else flow control statement 

(I)   switch....Case.... flow control statement

(III)  while.... flow control statement 

(IV)  for flow control statement 

(V)  foreach flow control statement

(VI)  Try.....catch flow control statement

(VII)   Declare flow control statement

(VIII)    Exit and return flow control statement

(XI)    Goto flow control statement 

◆   IF.......ELSE :-

The basic if statement can assign a value or perform other tasks based on the out come of a simple (or sometimes complex) test. The following Code sample uses are called If statement.

If....Else statement
If...else statement example

       The if statement checks the truthfulness of an expression and, if the expression is true, evaluates a statement. An if statement looks like:-

if ( expression ) statement 

To specify an alternative statement to execute when the expression is false, use the else keyword:-

if ( expression ) 
else statement

For example:- 

$ today = day ("l") ;
If ($ today == "Tuesday")
$ tax_rate = $ tax_rate +4 ;


Notice here that the condition being tested (whether it is Wednesday) is tested with double equals signs. Assignments are made with a single equals sign and tests of equality are done with two of them. 

       If you want to test for equality at the data type level, you can use the = = = test, which evaluates both the content of the elements being tested and the data types of those items. Consider this code:

 if (1 == '1') echo "true 1 equals '1' <br/>"; 
if (1 === '1') echo "true 1 equals '1'"; 
else echo "false 1 does not equal '1' " ; 

   This produces the following output, showing that when a string value is compared with a numerical value with = = , the string is converted to a number before the evaluation is performed, thus producing a true result. When comparison is performed with = = = (triple equal), no numerical conversion is done and the evaluation is performed based on both content and data type. Here is the output: 

true 1 equals ’1’ 
false 1 does not equal ’1’

◆  While.... :-

The while statement this statement will run code repeatedly as long as a condition remains true. The syntax for this takes two basic forms. First is the straight while statement, which looks like this:

First syntax example:-

$repeat = 1 ; 
while ($repeat <= 25) {
echo "the counter is: " . 
$repeat . "<br/>" ;
$repeat ++ ; 

The second syntax style ( do.. .while .. . ) Example:-

$repeat = 0 ; 
do { 
$repeat ++ ; 
echo "the counter is: " . 
$repeat . "<br/>" ; 
} while ($repeat <= 25);

◆       Switch....Case..... :-

The value of a single variable may determine one of a number of different choices (e.g., the variable holds the username and you want to do something different for each user). The switch statement is designed for just this situation.

       A switch statement is given an expression and compares its value to all cases in the switch; all statements in a matching case are executed, up to the first break keyword it finds. If none match, and a default is given, all statements following the default keyword are executed, up to the first break keyword encountered.

For example:-

foreach :-

The foreach statement allows you to iterate over elements in an array. The two forms of the foreach statement where we talk in more depth about arrays. To loop over an array, accessing the value at each key, use:

 foreach ( $array as $current ) 
{ // ...


The try...catch construct is not so much a flow-control structure as it is a more graceful way to handle system errors. For example, if you want to ensure that your web application has a valid connection to a database before continuing, We could write code like this:

Example :- 

Declare :-

The declare statement allows you to specify execution directives for a block of code. The structure of a declare statement is:

declare ( directive ) statement 

       Currently, there are only two declare forms: 

The ticks and encoding directives. You can specify how frequently (measured roughly in number of code statements) a tick function registered when register_tick_function() is called using the ticks directive.


◆.    Exits and return :- 

The exit statement ends execution of the script as soon as it is reached. The return statement returns from a function or, at the top level of the program, from the script.

Example :- 

Goto :- 

The goto statement allows execution to “jump” to another place in the program. You specify execution points by adding a label, which is an identifier followed by a colon ( : ).
   You then jump to the label from another location in the script via the goto statement:


◆  For :- 

The for statement is similar to the while statement, except it adds counter initialization and counter manipulation expressions, and is often shorter and easier to read than the equivalent while loop. Read more 

For example :- 

$total = 0 ; 

for ( $i = 0 , $j = 1 ; $i <= 10 ; $i ++ ,

 $j *= 2 ) 

$total += $j ; 



Please comment and follow this site blog inbox