Download presentation
Presentation is loading. Please wait.
1
More Selections BIS1523 – Lecture 9
2
Logical Operators Sometimes we want to compare more than just 2 things together, or build more complex conditions based on many things. To combine relational expressions into more complex expressions, we use Logical Operators. Relational expressions can be combined with the AND or OR operators. Each returns true in a slightly different manner: (expr1) AND (expr2) Returns true only if both expr1 and expr2 are TRUE (expr1) OR (expr2) Returns true if either expr1 or expr2 are TRUE AND can also be written as && OR can also be written as ||
3
Examples For the following values of length and width, is the above expression true or false? $length = 10 $width = -10 TRUE $length = -10 $width = 0 FALSE
4
Examples $length = 10 $width = 10 FALSE $length = 15 $width = 5 $length = 20 $width = 15 TRUE If you include math the result of the math is used in the comparison
5
Examples Notice in the above examples we always use parenthesis when using logical operators, to ensure the relational operators are evaluated first In a series of if statements, it is possible for more than one to be executed. If length is -10, and width is 50, the first two lines will both be printed
6
Example In the previous lecture, we used a series of if statements to do data validation. You could combine these into a single statement using OR’s, though the error messages would be less precise. Could be written as:
7
Nesting IF statements The code within the {}’s of an if statement can be any number of lines long, it can also include additional “IF” statements. In the example to the right, the red line group is entirely within the “if” part of the first if statement. So they will only be executed if condition1 is TRUE. Group2 will only be executed if condition2 is TRUE, but it is also part of the “red line” group, so condition1 will also have to be TRUE. Group3 is in the else part of the first condition, so it is executed only if condition1 is FALSE.
8
Nested If Example Take the following example, where we want to assign a discount based on some parameters: Customer code can be “R” or C” Discount is calculated by: “R” class customers get a 10% discount if they spend more than 100$. “C” class customers get a 15% discount normally, or a 20% discount if they spend more than 200$. We could nest if statements to first see if a customer was an R or C, and then check the purchase amount to assign a discount
9
Discount Example
10
Alternative Form As with most if statements, this problem can be solved several different ways. The two examples below are equivalent: The one on the left is slightly more efficient.
11
Continued Example In this example, what happens if the customer code is something other than an R or C? We never checked to see if it was a C, so we could add a second check there.
12
Continued Example The logic in this example is:
If its an R, check range Else if it is a C, check its range Else, its an error. This construct allows us to pick one of a series of options (R, or C, or something else). Using nested if/elses accomplishes this.
13
Elseif You can combine the
Construct into a single “elseif” as shown to the right. It is functionally the same, but requires you to use one less set of {}
14
Example: No nesting, using Elseif
One final way to code this example would be to use only elseif’s. It is more efficient than a series of Ifs, and not quite as complex to write as nesting as there are less {}’s to deal with: Using elseif’s allows only one of the possible discounts to be assigned.
15
switch Another selection construct that allows us to execute one selection of code out of several, based on a condition, is the switch The switch statement compares a variable (or expression) to a list of things, when it finds one it matches, it executes a section of code. The “default” value is a “none of the above” conditions. Switch does allow the use of strings in its comparison. The ‘break’ command should be used to end each section of code
16
Switch example Below the string variable $grade is compared to each value, ‘A’, then ‘B’, then ‘C’, and an appropriate section of code executed. Only one of the sections of code is executed, so this is functionally similar to a series of if/elseif’s.
17
Checkboxes Checkboxes are a user input method that allow the user to select any number of options from a list of things. Each checkbox should have a different name. The name is used in PHP to determine if the checkbox was checked using isset
18
checkboxes Since any number of checkboxes can be checked, we normally want to have an if statement for each one. For example, if we wanted to count the number of languages, we could do: Since more than one thing can be checked, switch, or elseif’s are not appropriate for checkboxes.
19
Checked property You can include the checked property in your HTML if you want a checkbox to be selected at the start.
20
Checkboxes vs Radio Buttons
Allow any number of selections Only one can be selected per group Each has a unique name One name for each ‘group’ of radio buttons Value property not used (yet, we will use it later in the course) Value property is used to determine which one is selected Use isset($_POST[‘name’]) to test One IF for each checkbox If isset($_POST[‘name’]) $result = $_POST[‘name’] One IF statement for each possible value If ($result == value)
21
Multiple PHP Tags Can you have multiple sections of code in your HTML.
In this case the code is executed from top to bottom. This is useful in a few cases. Since PHP outputting HTML is slower than just plain HTML, if you have a lot of HTML it is better to not have it enclosed in php print commands It can also be useful for organization (covered later).
22
Single Page Programs We can use if statements to compress our pages into a single PHP enabled page. If you go to one of our “results” pages without first going to the HTML page, the variables will be empty. It would be like the user hit “submit” without typing anything in. The “submit” variable will also be empty. This value is passed when we click the “submit” button of the form. To create a single-page program, we have the page check to see if it was submitted, if not, print out the “INPUT” html, if it was submitted, do the “output” stuff.
23
Example Outline Example is payment_simple.php on K: drive in the lecture 9 folder
24
Payment Example To test to see if the program was submitted, we can use this if: If it is not set, we want to print out the entire HTML for the input form. Since there are no variables, and no single quotes in it, we can cut & paste, and enclose it in single quotes…. Escaping single quotes might be necessary if your HTML included any.
25
Payment Example Then we add an ‘else’ to that, that needs to print out ALL the html for the result screen, as well as the PHP
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.