PHP: Conditional Statements

Derek Bridge

Department of Computer Science,
University College Cork

PHP: Conditional Statements

Aims:

Some of the guestbook (X)HTML again

<form action="guestbook.php" method="get">

 <label for="firstnameField">First name:</label>
 <input type="text" name="first" id="firstnameField" />

 <label for="surnameField">Surname:</label>
 <input type="text" name="surname" id="surnameField" />

 <input type="submit" value="Submit" /> 
 <input type="reset" />

</form>

The guestbook.php script

  • The data submitted by the user is available in an associative array: $_GET
    • The keys are whatever you used for the name attributes in the (X)HTML
    • The values are whatever string the user entered
    • E.g. $_GET['first'] accesses whatever the user typed as first name
    • Even if the user entered a number, the associative array will hold it as a string

The guestbook.php script

<?php echo '<?xml version="1.0" encoding="UTF-8"?>'; ?>
<!DOCTYPE html 
 PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <title>Welcome!</title>
</head>

<body>
<p>
 <?php 
  echo "You are {$_GET['surname']}, {$_GET['first']} {$_GET['surname']}."; 
 ?>
</p>

</body>
</html>

And POST?

  • If you want to use the POST command,
    • XHTML: in your form, use method="post"
    • PHP: in your PHP script, the data is available in an associative array: $_POST

Conditional statements

  • The conditional statement allows you to test whether a condition is true or false, and perform a different sequence of statements, depending on the outcome:
    if (condition)
    {
       do this if condition is true
    }

Conditional statements

  • age.html: an (X)HTML form that requests the user's age
  • testage.php: a PHP script that outputs different messages, based on the user's age
<?php
 if ($_GET['age'] < 18) 
 {
    echo 'CS1102 is a module for adults. Come back when you\'re older.';
 }
 if ($_GET['age'] >= 18) 
 {
    echo 'Welcome to CS1102!';
 }
?>

Conditional statements

  • We can use else to specify a sequence of statements to be executed when the condition is false:
    if (condition)
    {
       do this if condition is true
    }
    else
    {
       do this if condition is false
    }
<?php
 if ($_GET['age'] < 18) 
 {
    echo 'CS1102 is a module for adults. Come back when you\'re older.';
 }
 else 
 {
    echo 'Welcome to CS1102!';
 }
?>

Comparison operators

  • To compare the values in $x and $y, the operators are:
    $x == $y$x != $y
    $x < $y$x >= $y
    $x > $y$x <= $y
  • Class exercise: Why not use $x = $y instead of $x == $y$ to test for equality?
  • Class exercise: There is something strange about comparing $_GET['age'] to 18. What?

Comparison operators: advanced observations

  • PHP does numeric comparisons provided the two operators are numeric or are strings that are entirely numeric, e.g. "18"
  • PHP does lexicographic comparisons if at least one of the operators is a string that is not entirely numeric, e.g. "Hugh", "CS6120", "4C"
  • To force lexicographic comparisons, use strcmp($s1, $s2)
  • To return true if and only if $x and $y are equal and of the same type, use $x === $y
  • To return true if and only if $x and $y are not equal or not of the same type, use $x !== $y

Logical operators

  • && (also and)
    • e.g. if ($age > 16 && $age <= 66) ...
    • returns true if and only if both conditions are true
  • || (also or)
    • e.g. if ($age == 17 || $age == 18) ...
    • returns true if and only if either condition is true
  • !
    • e.g. if (! $age >= 21) ...
    • returns true if and only if the condition is false

Example: questionnaire.html

<form action="personality.php" method="get">
  <p>
   Which one of these best describes your personality?
  </p>
  <p>
   <input type="radio" name="pers" value="lust" id="lustBtn" checked="checked" />
   <label for="lustBtn">Lustful</label>
   <input type="radio" name="pers" value="sloth" id="slothBtn" />
   <label for="slothBtn">Slothful</label>
   <input type="radio" name="pers" value="greed" id="greedBtn" />
   <label for="greedBtn">Gluttonous</label>
  </p>
  <p>
   Check the box if you ever have murderous thoughts?
  </p>
  <p>
   <input type="checkbox" name="murder" value="yes" /> 
  </p>
  <p>
   <input type="submit" value="Submit" />;
  </p>
</form>

Checkboxes

  • A box that the user can 'check'
    <input type="checkbox" name="murder" value="yes" />
    • type="checkbox"
    • if the user checks the box, then the name and value are sent to the server
    • if the user does not check the box, no data is sent
    • by default, the box is initially unchecked
    • use checked="checked" attribute if you want it initially to be checked
  • More to be said in the case where you want a group of checkboxes: next lecture

Radio buttons

  • Like checkboxes, but usually used in groups: only one of the group can be checked at a time:
    <input type="radio" name="pers" value="lust" />
    <input type="radio" name="pers" value="sloth" />
    <input type="radio" name="pers" value="greed" />
    • type="radio"
    • if the user checks one, then its name and value are sent to the server
    • if the user does not check any, no data is sent
    • by default, the box is initially unchecked
    • it is common to use checked="checked" attribute so that one is initially checked

Example: personality.php

Following extensive analysis of your personality, we offer you this advice:
<?php
 $pers = $_GET['pers'];
 $murder = $_GET['murder'];
 if ($pers == 'greed' && $murder == 'yes') 
 {
    echo 'Be careful. One day, you might eat someone.';
 }
 elseif ($pers == 'sloth') 
 {
    if ($murder == 'yes') 
    {
       echo 'Don\'t worry. You\'ll never murder anybody: ';
    }
    echo 'You\'re a lazy b*****d!';
 }
 else 
 {
    echo 'You\'re not an interesting person. Go away.';
 }
?>

Notes on personality.php

  • Use of && in one of the conditions
  • Use of if...elseif...else...
  • Nesting one if inside another