ElearningWorld.org

For the online learning world

Elearning WorldTechnical

Decisions

Introduction

My last three posts have been about learning PHP, so this month I thought I’d continue on a smaller scale. Given the current situation and the lack of decisions we have about our lives, I thought about the ‘switch’ statement that can be used as a cleaner way of implementing branching when you have many choices.

If you’ve not read my posts on the ‘The first step’ then they are: one, two and three, then they will help, especially part one to have the environment upon which to run the code presented here.

Disclaimers

Firefox® is a registered trademark of the Mozilla Foundation – www.mozilla.org/en-US/foundation/trademarks/policy.

Ubuntu® is a registered trademark of Canonical Ltd – ubuntu.com/legal/intellectual-property-policy.

“Raspberry Pi” is a trademark of the Raspberry Pi Foundation – www.raspberrypi.org/trademark-rules.

Apache and Apache HTTP Server are trademarks of The Apache Software Foundation – www.apache.org.

Other names / logos can be trademarks of their respective owners. Please review their website for details.

I am independent from the organisations listed above and am in no way writing for or endorsed by them.

All example ‘code’ presented is written by myself, please feel free to copy for educational purposes only.

If it does not work

If something goes wrong, then you need to look in the error log file. On the Pi, this will be located in the ‘/var/log/apache2’ folder as ‘error.log’ and you can use the command ‘tail’ to see the most recent events as the file is appended to. In other systems, hunt around for ‘error.log’.

Making a decision

Perhaps the most key thing about a modern logical computer is its ability to make decisions based on data. Those decisions are logical and reduce down from the complex to a simple ‘yes’ or ‘no’. From my understanding this has its roots with Alan Turing and is proof “On Computable Numbers, with an Application to the Entscheidungsproblem.” (en.wikipedia.org/wiki/Turing%27s_proof), which whilst I understand computer architecture and software engineering, the mathematics is beyond me.

So what if we want a computer to do something in response to an input, where that input comes from the user with the options: ‘Stay’, ‘Go’ and ‘Make a cuppa’. Where only one is picked then we can use a ‘switch’ statement (www.php.net/manual/en/control-structures.switch.php). We could also use an ‘if’ statement (www.php.net/manual/en/control-structures.if.php), but that can begin to look messy as we will see later.

The code

Please note: Only run this on a local server with no access from the outside world as there is no validation undertaken on the input, this is a ‘security’ risk.

Firstly the ‘switch’ version:

<!doctype html>
<html>
    <head>
        <title>Switch</title>
    </head>
    <body>
<?php

global $_POST;
if (!isset($_POST) || empty($_POST)) {
    echo '<form action="./switch.php" method="post">';
    echo 'Your decision:<br>';
    echo '<input type="radio" name="decision" id="stay" value="stay">';
    echo '<label for="stay">Stay</label><br>';
    echo '<input type="radio" name="decision" id="go" value="go">';
    echo '<label for="go">Go</label><br>';
    echo '<input type="radio" name="decision" id="cuppa" value="cuppa">';
    echo '<label for="cuppa">Make a cuppa</label><br>';
    echo '<input type="submit" value="Submit">';
    echo '</form>';
} else {
    switch ($_POST['decision']) {
        case 'stay':
            echo '<p>Staying in</p>';
        break;
        case 'go':
            echo '<p>Going out</p>';
        break;
        case 'cuppa':
            echo '<p>Making tea</p>';
        break;
        default:
            echo '<p>Invalid data!</p>';
    }
    echo '<p>Refresh in ten seconds....</p>';
    header('Refresh: 10; url=switch.php');
}

?>
    </body>
</html>

which when run on the web server gives:

The decision on the web page

and:

Output of the decision

You can see that the statement is good at comparing values of the same thing, now if we look at the ‘if’ statement version:

<!doctype html>
<html>
    <head>
        <title>If</title>
    </head>
    <body>
<?php

global $_POST;
if (!isset($_POST) || empty($_POST)) {
    echo '<form action="./if.php" method="post">';
    echo 'Your decision:<br>';
    echo '<input type="radio" name="decision" id="stay" value="stay">';
    echo '<label for="stay">Stay</label><br>';
    echo '<input type="radio" name="decision" id="go" value="go">';
    echo '<label for="go">Go</label><br>';
    echo '<input type="radio" name="decision" id="cuppa" value="cuppa">';
    echo '<label for="cuppa">Make a cuppa</label><br>';
    echo '<input type="submit" value="Submit">';
    echo '</form>';
} else {
    if ($_POST['decision'] == 'stay') {
        echo '<p>Staying in</p>';
    } else if ($_POST['decision'] == 'go') {
        echo '<p>Going out</p>';
    } else if ($_POST['decision'] == 'cuppa') {
        echo '<p>Making tea</p>';
    } else {
        echo '<p>Invalid data!</p>';
    }
    echo '<p>Refresh in ten seconds....</p>';
    header('Refresh: 10; url=if.php');
}

?>
    </body>
</html>

you can see that it is functionally the same, however it could be considered less readable. But if you’re not looking at the same thing, then it is more flexible.

Conclusion

The basic language constructs such as ‘if’ and ‘switch’ are fundamental in automating the decision making process when turning tasks from a human to a computer driven one and form the basis of software.

Further reading

To learn more about PHP, you could choose to visit:

Gareth Barnard
Latest posts by Gareth Barnard (see all)
blank

Gareth Barnard

Gareth is a developer of numerous Moodle Themes including Essential (the most popular Moodle Theme ever), Foundation, and other plugins such as course formats, including Collapsed Topics.

One thought on “Decisions

  • Interesting!
    As a non-programmer I am comfortable with if’s (years of writing complex Excel formulae!) but I didn’t know about Switch.

    Reply

Add a reply or comment...