if elseif else if elseif else application switch

Day 3

Welcome to PHP in 15 Days – Guaranteed Course day 3. Today we are going to study conditional statements i.e. if, elseif, else and switch. These statements are really interesting and easy to understand with little attention. Also today, we are goingto develop our first ever application,which will take care of different scenarios. Like ourapplication will work keeping the days in consideration and will greet you differently for different days of the week. I hope you are excited enough to start, so, let’s go.

If, elseif, else

Very often we face the situation when we want our code perform different things on different inputs. For example, we define a variable and this variable can have any value of any data type. We want to show the output according to the data type. So, if variable has the data type ‘string’ our code should echo ‘It’s a string’, if the variable is containing integer, our code should echo, ‘It’s an integer’. OR let’s assume, our variable can have any value from 1 to 10 and we want to echo different messages for each number. So, these are very basic examples, where we might be in need of if, elseif and else statements. So, these statements help us in making decisions on different scenarios. We check the condition with the help of these and if the condition returns true that particular statement executes.

·         if: Checks the arguments given and returns true or false.

·         Only at the time when condition returns true,we can perform something.

·         elseif: If we want to check another argument we use elseif.

·         else: else is used to do something by-default if none of the above conditions/argumentsreturn true for if and elseif.

·         NOTE: elseif and else are always follows if statement respectively.




Let’s first have look at Syntax of this conditional statement

First of all write a Keyword if and then parenthesis and between parenthesis we put some argument.

if(some argument == true){
Do something;
}

 

After parenthesis we need to start curly braces and within curly braces we can write some code. This code will only be executed if the argument will return true. So, this is all for if condition. So, just keep in mind that the argument or condition we write between parentheses must return true, otherwise if will not execute the code we write within curly braces. Now considerthe if statement returns false and our code will obviously not run, but still we want to show something or we want to perform something otherwise. Here in this situation we can declare else. So, else only takes the script which we want to execute. else doesn’t check the conditions. Every time the if statement does not execute, else, will be.

if(some argument == false){
1: Do something;
}

else{
2: Do somthing;
}

Write ‘else’ keyword and then curly braces and between these we write the code to execute.

Now if the argument provided for if becomes false first ‘Do something’ will not work insteadthe execution area of else will be executed. So whenever argument of if is false else will be executed.

Let’s give numbering to the executions of each section, to make them more readable.

Now we want to check another condition. As I told earlier we can have several conditions to check, so, now suppose we have another condition to check.

if(some argument == false){
1: Do something;
}

 

elseif(some argument == false){
2: Do somthing;
}

else{
3: Do somthing;
}

 

Write the keyword ‘elseif’. Remember when we write if or elseif, we need to start parentheses right after these keywords and between parentheses we put the condition or conditions. After declaring the condition, same like before we start the curly braces and enclose the code to be executed only when this elseif statement would return true.

Now, what will happen here, first of all‘if’ condition will be checked and if it returns false, elseif will check the condition and if this is true 2 will be run otherwise 3 will be run. That’s pretty straight forward. This is all for the syntax.

So, now let’s just try to understand this whole processin another way. So, here within if statement we can get the result of any condition either true or false. So, we might have only true here or false. So, let’s just put true here and within execution area, let’s write ‘If condition will run.’ And then else and in this execution area, ‘This will not run.’

if(true){
echo 'If condition will run';
}
else {
echo 'This will not run';
}

 

Let’s go to the browser and see what happens. You can see ‘If condition will run’ message here. It means ‘if’ statement ran and else couldn’t. So, only one statement will run and the sequence will remain from if statement to else statement.

Now, let’s make this if statement false and then obviously ‘else will be executed.’ So, let’s change this to ‘not run’ and let’s make a change in else execution too. Instead ‘this’, just write ‘Else’ and just remove this not from here.

if(false){
echo 'If condition will not run';
}
else {
echo 'Else will run';
}

 

Let’s get back to the browser and you can see ‘Else will run’ message. So, now you knowwhen ‘if’ gets the false here within parentheses, whether you write it directly or it’s the result of condition, ‘if’ will not run rather else will run.

Now let’s write ‘elseif’ and this always stays between ‘if’ and ‘else’. Let’s provide it true and assume the condition we provided returned true, so here we want to echo ‘Elseif statement will run.’. You can see above that‘if’ statement has false, so it wouldn’t run and‘elseif’ statement now has true in here so, it must run. Let’s check this out.

if(false){
echo 'If condition will not run';
}
elseif(true){
echo 'Elseif statment will run'
}
else {
echo 'Else will not run';
}

 

Refresh, and we have some syntax error, get back to code and off-course we missed a semicolon here.

if(false){
echo 'If condition will not run';
}
elseif(true){
echo 'Elseif statment will run';
}
else {
echo 'Else will not run';
}

 

Let’s go to the browser, refresh and this time you can see we have the message of ‘Elseif statement will run.’

Now let’s make ‘elseif’ statement false as well. Now, if and elseif both are false. So, in this case else should run. Let’s check this in browser and we get the message ‘Else will not run’ but we have this message means else was run. Let’s correct this to ‘Else will run.’, go back again, refresh and ‘Else will run’ message. Let’s remove ‘not’ from ‘if’ statement too.

if(true){
echo 'If condition will run';
}
elseif(true){
echo 'Elseif statment will run';
}
else {
echo 'Else will run';
}

 

Now let’s consider one last scenario that we can face is, make both of these true. Put true here with ‘if’ statement and true with ‘elseif’ statement as well. Now the script will give the priority to the statement that comes first. It means first come first served. So, ‘if’ statement always stays ahead of ‘elseif’, that’s why ‘if’ statement will be executed. Let’s check this out in browser.

if(true){
echo 'If condition will run';
}
elseif(true){
echo 'Elseif statment will run';
}
else {
echo 'Else will run';
}

 

Rightly so, ‘If condition will run.’ Message here. Also, if we define several ‘elseif’ statements, and ‘if’ statement is false, the first ‘elseif’ statement will be run having resultant true within its parentheses.

‘END’

Now, I am going to write our very first PHP program and what this program will do for us is

It will check, what is day today and iftoday is Sunday,

Then say “Good Morrning, Today is not a working day. Sorry to disappoint you”.

If today is Monday,

Then say “Good Morning, Today is Monday. Too much work to do, hurry up”.

If today is any day other than Sunday or Monday,

Then say “Good Morning. Today is a normal working day. Cheers”!

 So, this is our small “day-check greeting application” and we are going to develop it in our PHP language right now.

First thing we need is to grab the days from the system time or some other resource. Let’s define a variable ‘x’ where we are getting the days. We will cover, ‘how to grab days or time’ in another tutorial in coming days soon. For now, just assume, we are grabbing day name here and storing it in a variable.

$x = '';

 

right nowx is defined but empty.

Now start our first conditional statement by if and then parenthesis and between these let’s put a comparison statement which we studied in one of our previous day session. This will return either true or false.

if($x == 'Sunday')

And now, we need to show a message of greeting with some instructions. Between curly braces

echo'Good Morning! Today is not a working day. Sorry for inconvenience.';

 

So what this statement doing is, it checks if our variable x equals to or has the value, Sunday, it will output the statement otherwise not. Always remember that after defining the condition use curly braces to write the code to be executed. If you need to write only one statement or line you can do it without curly braces but I recommend you to always encloseyour script within curly braces.

Let’s check it in our browser, and you can see our script is not outputting anything. Why? Because our variable is not equals to Sunday, its empty right now, let’s make it Sunday.

      $x = 'Sunday';

Let’s go back to our browser and there we go its greeting us now.

Now let’s proceed to the second stepof our application development.

elseif($x == 'Monday')

In case our first conditional statement ‘if’, returns false then we want to check whether the x has the value ‘Modnay’. That’s why we are using ‘elseif’, that allows us to define another condition. So, if this comparison statementreturns true then show greetings according to Monday.

echo'Today is Monday. Too much work to do, hurry up.';

 

Let’s check this out in the browser and here we go, it still saying according to Sunday, let’s make xequals toMonday

$x = 'Monday';

and here we go we get the greetingsfor Monday.

And now the final part of our application, if our script finds all the other days except Sunday and Monday, it will output something different from our first two outputs.

For this just put

else

 

asnow we do not need to check any condition or comparison here, we just want an output if both of the above conditions return false. So you can say, this as the default condition, when all the above conditions fail, this will run.

echo'Good Morning. Today is a normal working day. Cheers!';

 

and now let’s change our variable to Tuesday.

And here we go, ‘Good Morning. Today is a normal working day. Cheers!’

 

Complete Code

 

$x = 'Tuesday';

if($x == 'Sunday'){
echo 'Good Morning! Today is not a working day. Sorry for inconvenience.';
}

 

elseif($x == 'Monday'){
echo 'Today is Monday. Too much work to do, hurry up.';
}

 

else{
echo 'Good Morning. Today is a normal working day. Cheers!';
}

 

One thing to keep in mind, the hierarchy you must comply with.If you only want to use if and else, if will always proceed and else will follow. If you need to use if, else if and else you will keep the sequence. So here is a tip, keep if statement always at the top and else statement at the end and if you want to check more conditions your elseif statements will reside between if and else.

If you change $x to anything else, it will output same that is set for other days.Though this is not an extremely good program as it requires more flexibilities to be included but to give you a real world application touch at this point it’s really a nice one. This is the actual thing we do to developweb programs and applications. We extensively usemany conditional statements and several conditions inside theseto develop the procedural working of the web-applications keeping different factors in consideration. I highly recommend you to practice these conditional statements for as many times as you can and practicethem combining with comparison and logical statements.

Switch Statement – Conditional Statement

There are times when we want to perform different things based on one single variable. When this is the case switch statement is best option to use. Switch statement is a frequently used conditional statement in PHP. This conditional statement more or less works same like if, elseif and else, but it performs conditional check for only one input.

Let’s have a look at its syntax



To start this conditional statement we write ‘switch’ keyword. Right after this keyword we start parenthesis and between parentheseswe put a variable or some value on the basis of whichwedo or do not do something.

Now start curly braces. All conditional checks and executions will stay in this area. Now we write another keyword, case and then space and thensome value or variable. Case means, if the input value here in the parenthesis equals to this case value then we want our script do something here otherwise, script will move on to check other cases. Value here may be a string or integer or of any data type. We define any action or execution here. ‘Some action goes here’. On the next line we write another keyword ‘break’. If this very case is matched, means, if the condition returns true and the script executes the action, this break will prevent the script to go further on all the cases and check all the conditions. So, this break is playing a very important role to make the script really efficient. As soon as the true case is found, break keyword stops the script right away.

You can place as many cases as you want but for now just place two cases with value1 and value2. Default is used to declare what to do if none of the above cases are true. So, the action defined here will be executed when script would have gone through all the above cases and found none of them true.

switch(some variable){
case value1:
Some action goes here;
break;

case value2:
Some action goes here;
break;

default:
Some action goes here;
break;
}

 

Let’s define a variable ‘x’. Now start the switch statement and put variable x as an argument.

$x = ‘’;;
switch($x){
}

 

Our whole switch statement will base on this variable. Now move on,declare case 1 and then colon. Here we defined if x equals to 1, we will get the output ‘It’s Sunday’.

case1:
echo 'It\'s Sunday';
break;

 

By the way, if you want to put an apostrophe you can either put double quotes outside and use it or you can use single quotes outside and then escape the apostrophe by a backslash before it and the apostrophe will be overlooked by PHP.

And then break so if the first case or any case which is true thisbreak keyword will stop the script preventing it from moving on to the cases ahead.

This is another tiny program or application which is almost like the application we developed earlier using if, elseif and else.

Now, same for another case and put 2 here and then colon. So, if it finds x equals to 2 just echo ‘it’s Monday’ and if this executes, break right afterwards.

At the end we writedefault keyword, which will be executed if and only if none of the cases aboveis true.This is going to output, ‘it’s not Sunday, not Monday. It’s a regular day’.

$x = ‘’;
switch($x){
case 1:
echo 'It\'s Sunday';
break;
case 2:
echo 'It\'s Monday';
break;
default:
echo 'It\'s not Sunday and not Monday, it\'s a regular day.';
break;
}

 

So, this is it for coding side let’s check this out in browser now. Go to the switch.php page and run it. Here you can see the message ‘it’s not Sunday and not Monday. It’s a regular day’. Let’s go back to our code and you can see variable x has been assigned an empty value. Let’s assign 1 to variable x and check the output in browser again and this time you can see ‘it’s Sunday’ message. If we assign 2 to variable x, it should show the message ‘it’s Monday’ and rightly so, we get thismessage. So, this is howswitch statement works and how we use it. With this today’s sessionis over. I’ll see you on day4 with new lessons. Take care.

Practice files Day #3

         Download 



Related Posts:

Bas Cost Budde
May 17, 2020 13:46pm Reply
This is looking good Saud Ashfaq. Something I would recommend my junior collegues. Do you have a proofreader for your posts? If not, can I propose to proofread them?
Saud Ashfaq
May 20, 2020 12:30pm
@Bas Cost Budde Thanks a lot. We don't have proofreaders at the moment but will have soon. Thanks once again.

Leave a reply

Required fields are marked *

Login to Post Comment