PHP: One-Armed Conditional Statements I

Derek Bridge

Department of Computer Science,
University College Cork

PHP: One-Armed Conditional Statements I

Aims:

Comparison operators

PHP's comparison operators

OperatorParaphraseExample use
==Equals$a == $b
===Identical$a === $b
!=Not equal$a != $b
!==Not identical$a !== $b
<>Not equal$a <> $b
<Less than$a < $b
>Greater than$a > $b
<=Less than or equal to$a <= $b
>=Greater than or equal to$a >= $b

Using Boolean expressions I

Class exercise: What's the output of each of the following?

  1. echo 2 < 3;
  2. echo 2 == 3;
  3. echo '2 == 3';
$my_age = 20;
$your_age = $my_age;
  1. echo $my_age == $your_age;
  2. echo $my_age < $your_age;
  3. echo $my_age <= $your_age;
$your_age = $your_age + 1;
        
$is_equal = $my_age == $your_age;
  1. echo $is_equal;
$is_less_than = $my_age < $your_age;
  1. echo $is_less_than;
$is_less_than_or_equal_to = $my_age <= $your_age;
  1. echo $is_less_than_or_equal_to;

Using Boolean expressions II: the conditional

Class exercise: What is the output of the following?

$my_age = 20;
$your_age = 23;
if ( $my_age < $your_age )
{
    echo '<p>';
    echo 'I\'m younger than you!';
    echo 'The difference is ' . ($your_age - $my_age) . ' year(s).';
    echo '</p>';
}
echo "<p>I am {$my_age} and you are {$your_age}.</p>";

Also, what is the output if 23 is changed to 17? What is the output if 23 is changed to 20?

Flowchart

[A flowchart showing a one-armed conditional.]

Some observations

Class exercise

Incomplete version of tax.php

$salary = (int) $_GET['salary'];

$tax = 0.0;
if 




    
echo "<p>Your tax is {$tax} eurines</p>";

More example of conditionals

Class exercise

Incomplete version of duty.php

$import_value = (int) $_GET['import_value'];
$days_away = (int) $_GET['days_away'];

$tax_rate = 0.1;














echo "<p>You must pay {$duty} eurines</p>";