Conditional statements in php

Go back to last tutorial

Show complete series till now

Hello friends,

In this tutorial, we will study about conditional statements (if else statements and switch statements). These statements are used to make decisions with respect to some condition. Lets take an example first:

We have a variable, say $a, and we want to print 'harish' if $a is equal to 'harish'. We will write code something like this:

  1. <?php
  2. $a = 'harish'; //initialize variable $a
  3. //check the value of a
  4. if ($a == 'harish') {
  5. //if $a is equal to 'harish' then print harish
  6. echo 'harish';
  7. }
  8. ?>

In this example, you can see that we are using a if statement for checking if value of $a is equal to 'harish' or not. Focus on the syntax:
if( condition ){..........program/task..........}

The if loop will run if the condition is true or non-zero or non-empty. Here is three words:

  1. true: meaning of this word is clear itself. As in example, we have used comparison operator '==' which will check that $a is equal to 'harish' or not. If yes then it will return true otherwise false. It means condition is true or false.
  2. non-zero: We can also experiment by putting 0 and non-zero numbers at the place of condition. In fact, 0 is considered as false and other non-zero letters are considered as true.
  3. non-empty: All empty or null variables are considered as false and other variables / values are considered as true. Please not that empty doesn't mean that you leave condition blank. Empty mean null or undefined.

For better understanding, please try all of the three types discussed above.

if else statement:

This statement is selected when we have to choose one thing out of two things. For example, we want to categorize students of a class on the basis of there marks. If a student gets less than 33 then he will fail otherwise he will pass. Now our script will be like this:

  1. <?php
  2. $marks = 23; // initializing the marks
  3. // change its value for different results
  4. if ($marks<33) {
  5. echo 'shit! you have failed';
  6. }
  7. else{
  8. echo "you have passed!!!";
  9. }
  10. // above code is equivalent to this code
  11. // remove multiline comment it to verify
  12. /*
  13. if ($marks<33) {
  14. echo 'shit! you have failed';
  15. }
  16. // >= means less than or equal to
  17. if ($marks>=33){
  18. echo "you have passed!!!";
  19. }
  20. */
  21. ?>

It is clear from example that one of two if or else will run after checking the value of $marks. else works when condition fails.

nested statement:

We can use new statement inside another statement. For example if we have to check if student got A+ grade or not then we write script something like this:

  1. <?php
  2. $marks = 23; // initializing the marks
  3. //change its value for different results
  4. if ($marks<33) {
  5. echo 'shit! you have failed';
  6. }
  7. else{
  8. // we are writing if else inside if else statement
  9. if ($marks>90) {
  10. echo 'you have passed and got A+ grade';
  11. }
  12. else{
  13. echo 'you have passed but not got A+ grade';
  14. }
  15. }
  16. ?>

elseif statements:

If we have more than two choices then we can use elseif statement. It is similar to nested statement. For example, we can write above example something like this:

  1. <?php
  2. $marks = 23; // initializing the marks
  3. //change its value for different results
  4. if ($marks<33) {
  5. echo 'shit! you have failed';
  6. }
  7. elseif ($marks>90) {
  8. echo 'you have passed and got A+ grade';
  9. }
  10. else{
  11. echo 'you have passed but not got A+ grade';
  12. }
  13. ?>

If the first condition fails (marks >= 33) then elseif statement checks its own condition (marks > 90) and if this condition also fails then finally else statement runs. We can write elseif statements any number of times. For example if we have four choices instead of three then we will use elseif statement twice. Try this example yourself and give grade to students.

Min marksMax marksGrade
030F
3140E
4150D
5160C
6170C+
7180B+
8190A
91100A+

switch statement:

If you have done above example yourself then you will found that you have to write conditions again and again. It was a small example what if I give you very large list? Now we will see a new conditional method called switch statements. First look at this code:

  1. <?php
  2. $name = 'harish';
  3. switch($name){
  4. case 'variable':
  5. echo "it is a variable";
  6. break;
  7. case 'harish':
  8. echo "this is my name";
  9. break;
  10. default:
  11. echo "this is nessary default case";
  12. break; // this statement is not required
  13. }
  14. ?>

This is a example of switch statement. Lets understand this example:

  • We are defining a variable named name
  • We are passing that variable to switch statement
  • switch behave like electric switch. It takes a value as input. It matches that value to its cases defined inside it. If value matches with any case the program until first time beak statement runs.
  • After running first time break statement program comes out from the switch loop.
  • Every switch statement must has a default case which runs if the input value does not match with any of the cases.
  • We do not require any break statement after default case. We can skip that one.
  • In other words, a switch statement is just like group of if else statements. It starts running with a matching (or default) case and ends with break or '}'.

Advantage of switch statement is that it reduces the length of our code. Now return to our student's grading example and try to solve that with switch statement. We will get this code.

  1. <?php
  2. $marks = 0; // initializing the marks
  3. //change its value for different results
  4. // we are rounding marks to one digit
  5. // keeping same grade marks together
  6. $m = ($marks - 1)/10;
  7. // intval changes floating value to integer value
  8. // for example 3.2 => 3 and 3.8 => 3
  9. switch (intval($m)) {
  10. // case continue untill break
  11. case 0:
  12. case 1:
  13. case 2:
  14. echo "F";
  15. break;
  16. // after break statement program leaves the switch loop
  17. case 3:
  18. echo "D";
  19. break;
  20. case 4:
  21. echo "C";
  22. break;
  23. case 5:
  24. echo "C+";
  25. break;
  26. case 6:
  27. echo "B";
  28. break;
  29. case 7:
  30. echo "B+";
  31. break;
  32. case 8:
  33. echo "A";
  34. break;
  35. case 9:
  36. echo "A+";
  37. break;
  38. default:
  39. // default case if none of the case satisfied
  40. echo "wrong input for marks";
  41. break;
  42. }
  43. ?>

From the program, it is clear that you do not have to write much code just like before. Lets know what is going on in the code:

  • First we are subtracting one from the marks to make first digit of each grade ( except F) same. For example, 50 becomes 49 and 100 becomes 99.
  • Now marks are divided by 10 to make marks in a range of -0.1 to 9.9. For example, 00 becomes 0.1 and 67 becomes 6.7
  • intval is PHP inbuilt function which returns the integer part of the number. For example, it return 3 for 3.4, 3.5 and 3.8. In short, switch is taking integer part as an input.
  • After it we are matching that integer with cases. As I told before the case will be continued until the break statement. So, case 0, 1 and 2 are pointing to same steps, i.e. prints 'F' because there is no break statement between them.

Exercise:

  • Try to modify these examples.
  • Think more problems and try to solve them if you find any difficulty then ask in comments.

In next tutorial we will study about loops in PHP. If you like it then please share it with your friend otherwise give your valuable feedback to improve this as well as future tutorial to help other people.

Don't forget to Subscribe for latest feeds.

Comments and feedback are welcome.

next tutorial



About Harish Kumar

Harish is an interested person in the field of web development and blogging. He works for the need of young web developers in learning various languages, latest technologies and other essential tips and tricks. If you need some help or you have some suggestion then you email him at harish@findalltogether.com without any hesitation. You can also suggest/demand for articles of your own choice.

Related Articles

In this part of free tutorial series, I am telling you about syntax, variables, data types, comments, constants and o...
Introduction to PHP
In this part of free step by step tutorial series I will teach you about for loop, while loop and do while loop of PH...
loops in PHP
In this part of free step by step tutorial series I will teach you about if, if else, nested and switch statements of...
Conditional statements in php

Login or Sign up to leave comment.