learning php in 8 weeks – week 3

Posted on at

Week 3: -Making choices

prowebsters php 1
First of all I welcome you to keep continue following this program. In this week (3rd) we learn making choices in our php programs. When you write a program you mostly make the program to make some choices for you. For example, if you want to create a detailed database of members in your website members in a php program, the program must do different things for the members of different countries. In this case all choices are based in yes and no.

How a computer makes a decision

How a computer makes decision is a simple process of yes no questions, 0/1 and true/false. And a computer only concludes the things we tell it as it is possible.

When creating a program in order to make decision we have to create some possible outcomes listed. For example we can ask computer if a number is greater than 50. Then the computer would answer with yes or no. we can add more options like higher, lower or equal for this example.

Now, how a computer knows to give which answer to us? For this, we use the Boolean logic(Boolean logic is a kind of decision which has four main operators to give the choices to a program). We ask the computer a series of yes no questions.

For example if we ask the computer that if a number is greater than 50 it gives the answer yes, but if the computer want to tell us that the number is not greater than 50, we then ask next question, is the number equal to 50? If the answer was yes we tell it to the user. If the answer was no then we ask again the next question that is the number lower than 50? Then we tell the answer to the users.

We do this or make this decision through the computer by two ways:

The first way we are going to ask the computer and get the answers is by using if. Here we compare our number as(x) to the value (in our example 50):


if ($x > 50)
echo “x is larger than 50″;
elseif ($x == 50)
echo “x is equal to 50″;
echo “x is smaller than 50″;


The above code is checking to see if x is greater than 50, if it was true it give (echo) that x is larger than 50. If it was not true then it goes to the code “ELSIF”, if in this case x was equal to 50, the php code shows the text “ x is equal to 50 “. But if was not true it goes to ELS code and shows that “ x is smaller than 50 “.

If there were only two possible option, you can remove ELSIF and keep IF and ELS lines. Or if you want nothing to happen when code is not true you only use IF and remove ELSE.


The second way we are going to make decision through our php program is using a switch loop (a php function use in place of man IF/ELSE) as the below example:

<?php switch ($x) { case $x > 50: echo “x is larger than 50″; break; case $x == 50: echo “x is equal to 50″; break; default: echo “x is smaller than 50″; } ?>

The above code functions the same way as previous code did. There are three options again, the x can be greater than 50, equal to or smaller than 50. In SWITCH loop case is use instead of nesting IF and ELSEIF and default is used instead of else. In the above code we used two cases and one default response to be used if none of the cases were true.

Original Source of this Article

About the author


Freelance Web and Graphic Designer, Blogger and Social Media Activist.

Subscribe 0