CS 199 Willamette University Spring, 2019
 
Programming in PHP, Databases with MySQL,
and Web Applications
 

Lab: Conditional Statements Part 1

Please read pages 25-32 of your textbook before starting this lab.

Simple if Statements

The format of a simple conditional statement is as follows:

    if (condition) {
      code to be executed if condition is true;
    }

In this statement, code is executed if the condition in question is true. For a visual representation of this process, refer to p. 26 of the textbook.

A simple condition in this statement arises by comparing two values (e.g., numbers or strings of characters). The six comparison operators (sometimes called relational operators) are shown in the following table.

Conditional Statement Function Output
$a == $b Equal TRUE if $a is equal to $b.
$a != $b Not equal TRUE if $a is not equal to $b.
$a < $b Less than TRUE if $a is strictly less than $b.
$a > $b Greater than TRUE if $a is strictly greater than $b.
$a < = $b Less than or equal to TRUE if $a is less than or equal to $b.
$a > = $b Greater than or equal to TRUE if $a is greater than or equal to $b.

Table source: php.net

Examples

For one practical example of when conditional statements can be used, refer to the textbook pages 27-29.

For a second example, consider the following code:

    $first = 100;
    $second = 47;
    $third = 78;

    if($first > third){
      echo 'TRUE';
    }

Because we have specified only the output "TRUE" given the condition is met, if the condition is not true, nothing will occur.

  1. Given the variables with values assigned, what will the code output when run?

  2. Change the values of $first and $third. Write down their new values and what the code outputs now.

  3. Now look at the two examples below.

    Code Segment A:

      $x = 5;
      $y = 10;
      if ($x == $y){
         echo 'hello';
      }
      if ($x = $y){
         echo 'goodbye';
      }
    

    Code Segment B:

      $x = 5;
      $y = 10;
      if ($x = $y){
         echo 'hello';
      }
      if ($x == $y){
         echo 'goodbye';
      }
    

    Compare these two Code Segments.

    1. Describe how the segments differ.
    2. Type these segments into a program, run the program, and report the output.
    3. Explain what happens in each, and why the different output is obtained.
  4. Suppose you retrieve three randomized numbers from a form. Use conditional statements in order to find and echo the smallest value.
    HINTS:

In many settings, the comparison of data seems reasonably clear:

  1. Experiment what happens with characters, if upper case letters are compared with lower case letters. In particular, write a program to test if the following comparisons are true:

       "a" < "b"
       "a" < "A"
       "A" < "a"
       "Cat" < "Dog"
       "Cat" > "Dog"
       "Cat" < "Car"
       "Car" < "Cat"
    

More Comparisons of Numbers and Strings

Comparisons of numbers and strings may require particularly careful attention. For example, consider the following tables of variable assignments and comparisons.

Initial Assignment of Values to Variables

Variable Value Type
$number1 7 number or integer
$number2 9 number or integer
$number3 12 - 5 number or integer
$string1 "7" character string
$string2 "9" character string
$string3 "12-5" character string

Results of Comparisons

Comparison Result
$number1 == $number1 true
$number1 == $number2 false
$number1 == $number3 true
$number1 == $string1 true
$number1 == $string2 false
$number1 == $string3 false
$number2 == $string1 false
$number2 == $string2 true
$number2 == $string3 false
$number3 == $string1 true
$number3 == $string2 false
$number3 == $string3 false
  1. Review the results of these comparisons carefully.

    1. When one number is compared with another number, when is the result true?
    2. When one string is compared with another string, when is the result true?
    3. When one number is compared with a string, when is the result true?

Else Statements

We can use 'if' statements with 'else' statements to make our code do even more. The syntax of if/else statements are as follows:

    if (condition) {
      code to be executed if condition is true;
    } else {
      some other code to be executed
    }

An early example in this lab considered the following code:

    $first = 100;
    $second = 47;
    $third = 78;

    if($first > third){
      echo 'TRUE';
    }

In the example, if the condition was not met, nothing was printed on the screen. However, with an else statement added, the program determines the condition is false and jumps to the else clause, printing text even if the original condition is not met.

You can find an textbook example on page 30 of your textbook. We can make up our own example as well.

    $name = "Tom";

    if($name == "Tom"){
      echo 'Hello Tom';
    } else{
        echo "I don't talk to strangers";
      }
  1. Check what is printed by this code segment.

  2. In the assignment statement, what happens if you do not have quotes around the name, is there a difference?

  3. The text is framed with double quote marks: " "

    1. What happens if you use single quote marks instead?
    2. The overall quote marks can be either single quotes or double quotes, as long as they match. Write echo statements to display the following text:
      • won't
      • can't
      • "Have a nice day"
      • I really like "CS199"!

      For additional ideas about quotes, consult p. 30 of the textbook.

  4. Write your own example of a simple if/else statement and describe the outcome given the variable value you set.

Capitalization Revised

Issues of capitalization arise in two contexts: The use of upper and lower case letters in variables and the use of these letters in data.

  1. In a program, try comparing variables: '$Value' and '$value' and explain your findings.

In step 5 of this lab, you experimented comparing "a" with "A". Hopefully you discovered that these characters are considered different. However, in PHP, the function 'strcasecmp' allows for case-sensitivity to be overlooked.

if (strcasecmp($num1, $num2) == 0) {
    echo '$num1 is equal to $num2 in a case-insensitive string comparison';
}

More generally,

  1. For each string example in step 5 of this lab, use the strcasecmp function to compare the strings, and report what results you obtain.

Comparing 3 numbers

  1. Step 4 of this lab asked you to find the smallest of three numbers.

    1. Expand upon that work to write a program that outputs the smallest of three values but using if/else statements instead of just multiple if statements.
    2. In reflecting on your code in step 4 and 13a, what advantages and disadvantages to these two ways of finding the smallest value?

Elseif Statements

We can use an if/else statement followed by another 'if', but we can also streamline this command through the use of the handy elseif statements. By using multiple elseif statements, the code can check for multiple conditions.

Here's an example:

   $age= rand(16-30);

   if ($age < 18) {
      echo "sorry, you are not allowed to purchase alcohol";
   } elseif ($age < 20) {
      echo "you can vote as a teenager";
   } elseif ($age < 25) {
      echo "you are no longer a teenager, but cannot rent a car";
   } else {
      echo "you can rent a car";
   }

If none of the elseif conditions are met, the code will run through and execute the 'else' code.

  1. Rewrite your program for step 13a using elseif statements. Upon reflection, can you identify any advantages or disadvantages in using the elseif construction.
created 4 February 2019 by Zhen Zhen McMahon
modest editing 6 February 2019 by Zhen Zhen McMahon and Henry M. Walker
Valid HTML 4.01! Valid CSS!