Udjuni.com

udjuni.com search:




PHP Flow control functions


Most scripts we have looked at so far are linear and flow only in one direction. For example we have looked at scripts that would take two numbers add them together and display the answer. In real world things hardly work that way. In most cases you are faced with different possibilities and the actions you take is dependent on a predefined condition.


For example, if you were to supply a group of people with food that provide different nutritional value to different age groups, you would want to know what age group each individual falls and what food to give to each group according to their nutritional requirements. To demonstrate how this will work we will take a look at an example bellow:

Meal schedule:

People in age groups 10 – 15 years has to have 200g of calcium per day.
Pregnant women over the age of 35 must take in 500g Calcium per day.
People older than 50 regardless of gender must take vitamins A,B and C.

Note that the linear and single direction program flow would not work in this case. There are conditions to be met and depending on the conditions the program must execute different code or code components. To be able to program scripts that allow for such flexibility, the PHP language has what is called flow control functions to control the direction of the code execution depending on the available data and whether on not the data satisfies a set of predetermined conditions.

Bellow are some flow control functions used in PHP:

  • IF statement - As the name implies, the code enclosed in parenthesis following an if statement is only executed when the condition in the IF statement returns TRUE. If the condition evaluates to FALSE the code enclosed in the parenthesis following the else keyword is executed.
  • format for the IF statement:

    $age = 15;
    if($age >=10 && $age <= 15){
    echo “Must have 200g of Calcium per day”;
    }else{
    echo “check the other conditions and take appropriate action”;
    }

    This code here will evaluate the value in the variable $age, if that value is between 10 and 15 it will display Must have 200g of Calcium per day, otherwise display check the other conditions and take appropriate action . In this case, because we assigned the value 15 to the variable $age, the first statement will be executed. If we assigned the value 25 to the variable age, the statement enclosed in the ELSE parenthesis would be executed.

  • ( ? )Operator otherwise known as ternary operator - Is very similar to the IF statement, the returned value is derived from one of the expressions separated by a colon (:) . Example of ternary operator usage:
  • expression ? returned_if expression_is_true : 
    returned_if_expresion_is_false

    Now, let us try to evaluate our expression from the previous example using the ternary operator:
    $output = ($age >=10 && $age <= 15) ? “200g Calcium” : “Further evaluation”; 
              echo $output;
      
    This statement will evaluate whether the value of $age is greater than or equal to 10 AND less than or equal to 15. Since the assigned value (from previous example) is 15, the expression evaluates to TRUE and thus the first or expression to the left of the colon ( : ) will be returned and displayed on the screen.
  • Switch Statement - Unlike the IF statement, which if used with else if may yields multiple expression, the switch statement only evaluates to a single expression and executes different code components depending on the result of that expression. Bellow is an example of a switch statement usage:


  • switch ($gender){
             case ‘Male’ :  echo “person in question is a Man”;
                           break;
             case ‘Female’ : echo “person in question is a Woman”;
                           break;
             default : echo “the gender of the person in question could not be determined”;
                       break;
              }
    

    Which statement is displayed is dependant on the value of the variable $gender. If that Variable has the value “Male” then the statement person in question is a Man will be displayed. If the value is female then person in question is a Woman is displayed. Should the variable $gender not be assigned any value or if the value in the variable is neither “Male” nor “Female” then the statement the gender of the person in question could not be determined will be displayed.



RELATED ARTICLES:


What the cash flow statement tell us
The cash flow statement is a financial document that shows how changes in balance sheet and income statements affect cash and cash equivalents. As an analytical tool, the cash flow statement is useful in determining the short-te

PHP Flow control functions


The Anatomy of an IF statement
In programming, there are cases when you need the program to perform a certain function when a certain condition is met. If such conditions are met then a certain piece of code is executed, otherwise skip it and execute an alternative code. This is ac

Breaking out of loops with break statement
div class='floatleft'> So far, we have discussed how the while and for loops work. Assuming that you were looping through a set of values, dividing each value by a number between -10 and 10. Given that you are aware of the rule that "Anything divided by zero

while loop


for loop


Functions Notation


Controlling cash flow problems
Cash flow refers to the way cash moves in and out of the business. In general, cash comes in when customers buy the products and services, and moves out of the business when you pay your debt to banks, suppliers and utility bills. As you can already see from this, a few things have an impact on your

Array related PHP Functions
The PHP language has a lot of functions that are used to populate, retrieve and manipulate array variables. Bellow are some of those functions, we will be using some of them as we continue to learn. Functi

Functions in PHP
Just like we have learned in mathematics, the basic way to think of and understand functions is to think of it as a machine, you feed it with raw materials and the machine goes through processing these materials into finished and possibly usable product. An easy way to look at functions is to consid

Did not find it? Try udjuni.com search:







© 2009 UdjunI LLC. All rights reserved | Privacy policy | Tutorial Index | RSS Feed