Task Four - Input Validation - The IF Statement - PHP Tutorial

This program prompts you for two numbers and then divides them
before displaying the answer. It also tests to prevent the divide by
zero error. This is an example of Input Validation.

This program uses the "if" statement. There is more information about "if" here.

Here is the result of running the program with valid and invalid input data.

Here is the program code.


<?php

// ===== INPUT ===============================
echo "\n\nPlease enter a number ... ";
$N1 = fgets(STDIN);

echo "\n\nPlease enter a number ... ";
$N2 = fgets(STDIN);
// ===========================================

// ===== PROCESS =============================
if ($N2 == 0)    // VALIDATION
{
  echo "\n\nERROR - Can not divide by zero.";
  $output = "undefined";
}
else
{
  $output = $N1 / $N2;
}
// ===========================================

// ===== OUTPUT ==============================
echo "\n\nThe answer is $output\n\n\n";
// ===========================================

?>

Code

Explanation

if ($N2 == 0)

This is called an IF statement.

It tests to see if ($N2 is equal to zero).

The test gives a true or false result called a Boolean.

If the Boolean is true ($N1 was equal to 0) the block of code inside the first curly brackets runs.

If the Boolean is false, the block of code in the curly brackets after else is run.

Be careful not to confuse "=" with "==". The first is used to assign a value. The second is used to test for equality.

{
  echo "\n\nERROR: Can not divide by zero.";
  $output = "undefined";
}

Code inside curly brackets is called a block. Here are two statements inside the block. Both these statements are run if the boolean is true.

else

The else part of an IF statement is optional. If it is present, the block of code after else is run if the boolean was false.

{
  $output = $N1 / $N2;
}

Code inside curly brackets is called a block. Here is one statement inside the block. This statement is run if the boolean is false.

Learning Task

Write a program which prompts for your name and a number representing the hour of the day. The number will be between 0 and 23. Depending on the time of day, your program shoud reply with

  • Good morning {name}
  • Good afternoon {name}
  • Good evening {name}
  • Good night {name}

Numbers below zero and above 23 should be rejected and an error message displayed.

Comparison Operators

Example Name Result
$a == $b Equal TRUE if $a is equal to $b.
$a === $b Identical TRUE if $a is equal to $b, and they are of the same type. (PHP 4 only)
$a != $b Not equal TRUE if $a is not equal to $b.
$a <> $b Not equal TRUE if $a is not equal to $b.
$a !== $b Not identical TRUE if $a is not equal to $b, or they are not of the same type. (PHP 4 only)
$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.

 

Logical Operators

Example Name Result
$a and $b And TRUE if both $a and $b are TRUE.
$a or $b Or TRUE if either $a or $b is TRUE.
$a xor $b Xor TRUE if either $a or $b is TRUE, but not both.
! $a Not TRUE if $a is not TRUE.
$a && $b And TRUE if both $a and $b are TRUE.
$a || $b Or TRUE if either $a or $b is TRUE.