PHP: Two-Armed Conditionals

Derek Bridge

Department of Computer Science,
University College Cork

PHP: Two-Armed Conditionals

Aims:

The two-armed conditional statement

Class exercise: What is the output of the following?

$grade = 57;
if ( $grade < 40 )
{
    echo '<p>You have failed your degree.</p>';
}
else
{
    echo '<p>You have passed your degree.</p>';
}
echo '<p>Goodbye!</p>';

Also, what is the output if 57 is changed to 12?

Flowchart

[A flowchart showing a two-armed conditional.]

Class exercise

Writing a sequence of ifs instead of an if-else

Multiple ways of writing the same program

Class exercise

Your answer

$cont = (int) $_GET['cont'];
$exam = (int) $_GET['exam'];

$message = '';
if 

   

else

    
    

echo $message;

Class exercise

Incomplete version of gas.php

$tariff = $_GET['tariff'];
$units = (int) $_GET['units'];

$fixed_charge = 0.0;
$variable_rate = 0.0;
$total_charge = 0.0;

// Set the fixed charge and variable rate, depending on the tariff
if 

    
    



    
    



// If high consumption, add energy tax
if 

    


echo "<p>Pay us {$total_charge}, or we'll send the boys round</p>";