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.


Firefox® is a registered trademark of the Mozilla Foundation –

Ubuntu® is a registered trademark of Canonical Ltd –

“Raspberry Pi” is a trademark of the Raspberry Pi Foundation –

Apache and Apache HTTP Server are trademarks of The Apache Software Foundation –

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.” (, 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 ( We could also use an ‘if’ statement (, 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:

The HTML and PHP of the 'switch' statement

which when run on the web server gives:

The decision on the web page


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:

Using an 'if' statement code instead

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.


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)

Gareth Barnard

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

One thought on “Decisions

  • 16th May 2020 at 8:59 am

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


Add a reply or comment...