Presentation is loading. Please wait.

Presentation is loading. Please wait.

Conditions and Ifs BIS1523 – Lecture 8.

Similar presentations


Presentation on theme: "Conditions and Ifs BIS1523 – Lecture 8."— Presentation transcript:

1 Conditions and Ifs BIS1523 – Lecture 8

2 The if statement The if statement let’s us conditionally execute code.
The condition above is something that evaluates to either TRUE or FALSE. There can be any number of commands inside the {} If the condition is true, the statements within {} are executed. If the condition is false, the statements are skipped, and the program continues immediately following the closing }

3 Boolean Variables An additional type of variable is a “Boolean” variable. Instead of being a number, or a string, it has the value of TRUE or FALSE. To set the value of a boolean variable, just use an assignment statement. Notice the word TRUE does not have double quotes around it (if it did, it would be considered a string.) Boolean variables can be used as a condition in an if. So if the value of the variable $okay was TRUE, it would print out the message.

4 The empty function There are several boolean functions available for use in PHP. One such function is the empty function. the empty() function, which checks to see if a given variable has an “empty” value. A variable is considered to have an empty value if the variable has no value, has a value of 0, or has a value of FALSE. In any of these cases, the function returns TRUE; otherwise, it returns FALSE The isset() function is nearly the opposite of empty. It returns TRUE if a variable has any value (including 0, FALSE, or an empty string)

5 is_numeric function Another boolean function is is_numeric().
It tests a variable to see if it has a numeric value, if so it returns TRUE. A variable could incorrectly have a non-numeric value if we allowed the user to enter it. For example, the user might do something like:

6 Reversing a Condition The previous example was good at telling if a variable was a number, but what if we want to do something if it is NOT a number? The ! Operator reverses a condition (read aloud as the word “not”). So the condition above is the logical opposite of the previous example. If the variable $balance is not numeric, an error will be printed out. We can use the empty, is_numeric, and boolean variables to do data validation of our forms

7 Data Validation Whenever we ask the user to enter in information, we want to make sure as best we can that they entered in correct, or valid values. Common mistakes include leaving inputs blank, or typing in incorrect values (where we want numbers) We can’t check every possible case (who could tell if the word Frothi was a valid name or not?), but we can test to make sure at least something was entered, and we can test to see that numbers were actually numbers. We will accomplish this with a series of if statements to test our inputs, and report an error if any of them were incorrect.

8 Which function to use? As a general rule:
We will use is_numeric to test numeric values We will use the isset() function to validate non-text form elements like checkboxes, radio buttons, and select menus We will use the empty() function to validate text fields

9 Example Program As an example, lets look at the payment calculator program we did in Lecture 7. We have 3 different inputs, balance, charges, and credits. We want to make sure the user entered in values for all three of these.

10 Checking numeric We use a separate if statement for each input we want to test, printing out an appropriate error message for incorrect data

11 Preventing Calculations on Error
If the user then leaves data blank, we get one (or more) error messages stating what went wrong Notice, however, that we also get a printout for new balance and minimum payment. These are incorrect, since we got incorrect data to start with. To prevent these from printing out, we will use a FLAG variable

12 Flag Variables A flag variable, in its simplest form, is a variable you define to have one value until some condition is true, in which case you change the variable's value. For data validation, we will use it to tell if any of the data entered was invalid. The process is: Set the variable equal to TRUE before testing your data If it fails any of the tests, set it to FALSE After you have tested all your data, if the variable is still TRUE, then you know the data passed all the tests.

13 Flag Variable Example After we have checked every input, we can check the flag. We only want to do our calculations and output if the flag is TRUE (everything is OKAY). Remember, if statements can contain any number of lines, so we can put ALL our calculations and output within that last if

14 Flag variable example So, in the example below, we won’t do any calculations or output if the data was not OK.

15 Relational Expressions
In addition to functions, and boolean variables, a condition can also be a relational expression. Relational expressions compare 2 numbers, and return TRUE or FALSE The expressions are made up of one of the following relational operators: Relational expressions will have a number on each side of an operator.

16 Relational Expressions
Relational expressions do numeric comparisons. So in the above example, if the value of the variable $new_balance was numerically less than the value 0, the condition would return true. Using parenthesis, relational expressions can be negated with the ! Operator, but since each relational operator has a logical opposite, that is usually not necessary. The condition below is logically exactly the same as the one at the top of this slide

17 Opposite Relations The following shows each relational operators opposite: Be sure and don’t confuse the assignment operator (=) with the equality operator (==). They are not interchangeable. String comparisons are done with functions, so only use these relational operators for numeric expressions. Operator Counterpart == != > <= < >=

18 The else clause Often times we want to do one section of code if a condition is true, and another if a condition is false. Rather than write 2 if statements in a row, you can use an else clause. Only one of the two groups of statements can be executed. If the condition is TRUE it will be the first group, if the condition is false it will be the second group. One of the 2 groups of statements will be executed. It might be a different group each time you run the program, but one of them has to be executed.

19 Else example In our previous data validation example, we printed out the calculations if the data was okay. We could add an ‘else’ condition for further error reporting.

20 Another Else Example In the previous credit card example, the minimum payment was set to 2% of the new balance. However, most credit cards won’t have a minimum payment of only a few cents. So, we could check to see if the balance was less than $20, and if so, the minimum payment is equal to the entire balance.


Download ppt "Conditions and Ifs BIS1523 – Lecture 8."

Similar presentations


Ads by Google