Monday, January 29, 2018

Decision Making in PHP

Making Decisions


Like most programming languages, PHP lets you write code that can make decisions based on the result of an expression. This allows you to do things like test if a variable matches a particular value, or if a string of text is of a certain length. In essence, if you can create a test in the form of an expression that evaluates to either true or false, you can use that test to make decisions in your code.

You studied expressions, but you might like to quickly review the “Operators and Expressions”section in that chapter to give yourself an idea of the kinds of expressions you can create. You can see that, thanks to the wide range of operators available in PHP, you can construct some pretty complex expressions. This means that you can use almost any test as the basis for decision-making in your code.

PHP gives you a number of statements that you can use to make decisions:

•  The if statement
•  The else and elseif statements
•  The switch statement

You explore each of these statements in the coming sections.

◆  Simple Decisions with the if Statement

The easiest decision-making statement to understand is the if statement. The basic form of an if construct is as follows:

if ( expression )



// Run this code



// More code here


If the expression inside the parentheses evaluates to true, the code between the braces is run. If the expression evaluates to false, the code between the braces is skipped. That’s really all there is to it.

        It’s worth pointing out that any code following the closing brace is always run, regardless of the result of the test. So in the preceding example, if expression evaluates to true, both the Run this code and More code here lines are executed; if expression evaluates to false, Run this code is skipped but More code here is still run.

Here’s a simple real-world example:

$widgets = 23; 

if ( $widgets == 23 )



echo "We have exactly 23 widgets in stock!";

}

The first line of the script creates a variable, $widgets, and sets its value to 23. Then an if statement uses the == operator to check if the value stored in $widgets does indeed equal 23. If it does —and it should! —the expression evaluates to true and the script displays the message: “We have exactly 23 widgets in stock!”If $widgets doesn’t hold the value 23, the code between the parentheses —that is, the echo() statement —is skipped. (You can test this for yourself by changing the value in the first line of code and re-running the example.)

 Here’s another example that uses the >= (greater than or equal) and <= (less than or equal) comparison operators, as well as the && (and) logical operator:

$widgets = 23; 

if ( $widgets >= 10 && $widgets <= 20 )



echo "We have between 10 and 20 widgets in stock.";

}

This example is similar to the previous one, but the test expression inside the parentheses is slightly more complex. If the value stored in $widgets is greater than or equal to 10, and it’s also less than or equal to 20, the expression evaluates to true and the message “We have between 10 and 20 widgets in stock.”is displayed. If either of the comparison operations evaluates to false, the overall expression also evaluates to false, the echo() statement is skipped, and nothing is displayed.

The key point to remember is that, no matter how complex your test expression is, if the whole expression evaluates to true the code inside the braces is run; otherwise the code inside the braces is skipped and execution continues with the first line of code after the closing brace. You can have as many lines of code between the braces as you like, and the code can do anything, such as display something in the browser, call a function, or even exit the script. In fact, here’s the previous example rewritten to use an if statement inside another if statement:




The code block between the braces of the first if statement is itself another if statement. The first if statement runs its code block if $widgets >= 10, whereas the inner if statement runs its code block —the echo() statement —if $widgets <= 20. Because both if expressions need to evaluate to true for the echo() statement to run, the end result is the same as the previous example.

If you only have one line of code between the braces you can, in fact, omit the braces altogether:

$widgets = 23;

if ( $widgets == 23 ) 

echo "We have exactly 23 widgets in stock!";

However, if you do this, take care to add braces if you later add additional lines of code to the code block. Otherwise, your code will not run as expected!

◆   Providing an Alternative Choice with the else Statement :- 

As you’ve seen, the if statement allows you to run a block of code if an expression evaluates to true. If the expression evaluates to false, the code is skipped.

You can enhance this decision-making process by adding an else statement to an if construction. This lets you run one block of code if an expression is true, and a different block of code if the expression is false.

Example:-

if ( $widgets >= 10 )

 { 

echo "We have plenty of widgets in stock."; } else

{

 echo "Less than 10 widgets left. Time to order some more!";

}

If $widgets is greater than or equal to 10, the first code block is run, and the message “We have plenty of widgets in stock.”is displayed. However, if $widgets is less than 10, the second code block is run, and the visitor sees the message: “Less than 10 widgets left. Time to order some more!”

You can even combine the else statement with another if statement to make as many alternative choices as you like:





◆   The switch statements :- 

The switch statement is a very useful programming construct in scenarios where a single variable is to be tested for different values. The same programming scenario can be implemented using nested if statements. However, the switch is a more efficient option for this as it creates a table of values and according to the value received, the execution directly jumps to the code block corresponding to that value.The syntax for using the switch statement is as follows –

switch (expression){ case value1: -

Code block if the expression evaluates to a value equal to value1;

break;

case value2: -

Code block if the expression evaluates to a value equal to value2; break;

default:

Code block if the expression evaluates to a value that is not equal to value1 or value2;       }

Sample code that implements this concept has been given below for your reference. Read More








0 comments:

Please comment and follow this site blog inbox