If...
The first type of condition we will look at isAgain, the syntax is very close to ordinary English: If a condition is met, then execute something. Let's look at a simple example:
if ... else ...
The next type of condition will want to look at isAgain, the syntax is very close to ordinary English: if a condition is met execute something or else execute something else.
In lesson 4, you learned how to find the number of a month. In the following example, we will use the month number in an
show example
As you can see, this condition is not a particularly smart condition - it only works when it's March!
However, there are plenty of ways to improve the condition and make it more precise. Below are listed comparison operators that can be used in the condition:
== Equals
< Less than
> Greater than
<= Less than or equal to
>= Greater than or equal to
!= Not equal to
In addition, there are some logical operators:
&& And
|| Or
! Not
The operators can be used to develop more precise conditions, so now we can expand the above example to include all the spring months:
Let's take a closer look at the extended condition:
The condition can be translated into:
Smart, eh? Operators play a significant role in many different parts of PHP.
But it still only works with March, April and May. All other months are not yet covered by the condition. So let's try to develop the condition a little more.
if ... elseif ... else...
Usingshow example
To write conditions is all about thinking logically and being methodical. The example above is pretty straightforward, but conditions can get very complex.
switch ... case
Another way of writing conditions is to use theThis method is based on an expression and then lists different "answers" or "values" with related statements. The easiest way to explain the method is to show an example.
As you may remember from lesson 4, the function
show example
Often
In the next lesson, we will look at how you can add comments to your scripts to explain how they work. Good comments can be crucial if you or somebody else has to make changes in your codes at a later stage.