# PHP: Nested Conditionals

Aims:

• to learn about nested `if-else` statements
• to practice reasoning about supposedly equivalent programs
• to learn how to avoid the dangling-`else` problem

# Example

• Following a coup d'etat, the Nerdland tax laws have changed again. The following tables show the rate at which people must pay tax on income above a threshold
Thresholds Tax rates
SingleMarried SingleMarried
Female2000032000 Female30%25%
Male1800032000 Male40%25%
• Suppose `tax.html` contains this form:
```<form action="tax.php" method="get">
<input type="radio" name="status" value="single" />
<input type="radio" name="status" value="married" />
<input type="radio" name="sex" value="F" />
<input type="radio" name="sex" value="M" />
<input type="salary" name="salary" value="0" />
<input type="submit" />
</form>```
• The next slide shows one possible version of `tax.php`

# A program with nested-`if`s

```\$status = \$_GET['status'];
\$sex = \$_GET['sex'];
\$salary = (int) \$_GET['salary'];

\$threshold = 0;
\$tax_rate = 0.0;
\$tax = 0.0;

if ( \$status == 'single' )
{
if ( \$sex == 'F' )
{
\$threshold = 20000;
\$tax_rate = 0.3;
}
else // i.e. \$sex == 'M'
{
\$threshold = 18000;
\$tax_rate = 0.4;
}
}
else // i.e. \$status == 'married'
{
\$threshold = 32000;
\$tax_rate = 0.25;
}

if ( \$salary > \$threshold )
{
\$tax = \$tax_rate * (\$salary - \$threshold);
}
echo "<p>Send us {\$tax} eurines or your first-born child</p>";```

# Class exercise

• Rewrite the `if-else` to eliminate the nested-`if`
• Compare the two versions

# Class exercise

• Write the output of the following:
```\$x = 1;
\$y = 1;
if ( \$x == 2 )
if ( \$y == 2 )
echo '<p>\$x and \$y are both 2</p>';
else
echo '<p>\$x was not 2</p>';```

# The dangling-`else` problem

• Confusion can arise when a statement mixes a one-armed conditional with a two-armed conditional
• There will be two `if`s and one `else`
• So which `if` does the `else` belong to?
• PHP is uninfluenced by your indentation
• PHP has a simple rule:

The `else` belongs to the most recent `if`, unless braces dictate otherwise

• Class exercise: Rewrite the program using new indentation to show how the `else` actually related to the `if`s

# Avoiding the dangling-`else` problem

• Always include braces...and think carefully about where the closing brace goes
• E.g. this is what the programmer intended
```\$x = 1;
\$y = 1;
if ( \$x == 2 )
{
if ( \$y == 2 )
{
echo '<p>\$x and \$y are both 2</p>';
}
}
else
{
echo '<p>\$x was not 2</p>';
}```

# Avoiding the dangling-`else` problem

 ``` if ( B1 ) {     if ( B2 )     {         S1;     }     else     {         S2;     } } ``` ``` if ( B1 ) {     if ( B2 )     {         S1;     } } else {     S2; } ```

# Class exercise

• In the final-year of the BSc in Alchemy at the National University of Nerdland, students are judged on their exams, continuous assessment and a project, as follows Exams ≥ 40% Cont. ass. ≥ 40% Project ≥ 40% Pass Resit exams Resubmit cont. ass. Re-do project Fail Y Y Y Y N N N N Y Y N N Y Y N N Y N Y N Y N Y N × × × × × × × × × × ×
• Write `outcome.php`, which takes data from this form, and tells the student the outcome:
```<form action="outcome.php" method="get">
<input type="text" name="exam" />
<input type="text" name="cont" />
<input type="text" name="proj" />
<input type="submit" />
</form>```
• NB Think hard to make your script as concise and clear as possible