A Crash course on web development with PHP for beginners (Day 3)

Hi there! In this lesson we will continue with conditional statements which we started in day 2 of this course. Sure by now you can construct simple conditional structures in your code.

Just in case you’re wondering why our codes just have plain text and no graphics, colors and stuff, you should know that in web development HTML (Hyper Text Markup Language) and CSS (Cascading Style Sheets) are used to design the front end while PHP (Hypertext Preprocessor) is used to process data on the server.

Although PHP can be used to design the front end but it would require embedding the HTML inside it. We would treat a little HTML as we go on in this course. Personally I consider HTML as the simplest language yet so you need not worry.

Using Conditional Statements

A conditional statement executes a block of statements only when certain conditions are true. Here are two useful types of conditional statements:

  •  if statement: Sets up a condition and tests it. If the condition is true, a block of statements is executed.
  •  switch statement: Sets up a list of alternative conditions. It tests for the true condition and executes the appropriate block of statements. 

We treated a bit of “if statements” in the last lesson.  Let’s go a little further.

The general format of an “if conditional statement” is as follows:

if ( condition )
{
    block of statements
}
elseif ( condition )
{
    block of statements
}
else
{
    block of statements
} 
Using our “Rain” example but with the introduction of a new fact

Akin: If the rain stops before 9 or the bus comes, then we will go to school

John: And if neither happens?

Akin: Then we’ll watch a movie.

Translating the resulting action in would be

if ( the rain stops )
{
    Then we will go to school
}
elseif (the bus comes)
{
    Then we will go to school
}
else
{
    we’ll watch a movie 
} 
You can then translate this to PHP.  It’s quite easy

A more experienced programmer would rather do

if ( the rain stops OR the bus comes)
{
    Then we will go to school
}
else
{
    We’ll watch a movie 
}
OR in this case is a logical operator.

Logical Operators

Operator Alternative Meaning
AND && Both comparisons are true
OR || of the comparisons or both of the comparisons are true
XOR   One of the comparisons is true but not both of the comparisons

Logical Operators can hence be combined with comparison operators in an “if statement”. See the example below:

$score;
if ($score > 92 )
{
    $grade = "A";
    $message = “Excellent!”;
}
elseif ($score <= 92 and $score > 83 )
{
    $grade = "B";
    $message = "Good!";
}
elseif ($score <= 83 and $score > 74 )
{
    $grade = "C";
    $message = "Okay";
}
elseif ($score <= 74 and $score > 62 )
{
    $grade = "D";
    $message = "Uh oh!";
}
else
{
    $grade = "F";
    $message = "Doom is upon you!";
}
echo $message."\n";
echo "Your grade is $grade\n";
Set $score to various values to see the various output.

Nesting If Statements

This simple denote having an “If statement” inside another “If statement”. Nesting can have as many levels has required by the programmer. Careful though, too man can get confusing and create disastrous bugs in your code. 

Let’s create another scenario.

A Club bouncer is given an instruction to allow the following people into the club:

  1. Males who are 18 years or older who have a paid the entry fee of NGN2000
  2. Females who are 18 years or older

Suppose we are to create a PHP program to control the bouncer, it would look like this:

$age;      // age of the person
$entry_fee;    // entry fee
$sex;      // male of female

If($age >=18)
{
    If($sex == "female")
    {
          echo "Welcome, You can enter!";
    }
    else If($sex == "male" AND $entry_fee == 200)
    {
        echo "Welcome, You can enter!";
    }
    else
    {
         echo "You have to pay the entry fee!"
    }
}
else
{
    echo "You are too young for this club!"
}
This could also be written in shorter versions by experienced programmers. So you could try to shorten the code on your own. Although some shorter versions might not be as efficient as this one so be careful.

Using Switch Statements

For most situations, the “if conditional statement” works best. However, sometimes you have a list of conditions and want to execute different statements

Suppose you’re to convert a directory of states and their state codes into a PHP code where on supply of the state, the program must return its state code. We could have something like this:

<?php
$statename;  // name of the state
$statecode;  // code of the state

switch($statename)
{
    case "Lagos":
        $statecode = 01;
        break;
    case "Oyo":
        $statecode = 02;
        break;
    case "Delta":
        $statecode = 013;
        break;
    default:
        $statecode = 0;
        break;
}

echo "The state code for {$statename} is {$statecode}";
?>
If there is no case section for the value of $statename, the script executes the default section. You can use as many case sections as you need. The default section is optional. If you use a default section, it’s customary to put the default section at the end, but as far as PHP is concerned, it can go anywhere.

The break statement tells the computer to leave the switch section and go on with the program execution.

Notice the way we echo the result. In previous examples we did echo "The state code for ".$statename ." is ".$statecode; choose your preferred method.

I will stop here for today and let you play with the codes some more. Feel free to ask any question in the discussions segment or just leave a comment below.

A Web designer and developer. Speaks PHP, Java, Javascript, HTML

Leave a Reply

Your email address will not be published. Required fields are marked *