Best PHP Tutorial For Beginners (IF/Else Statements) Part- 3

Over the past two elements I actually have shown you the fundamentals of text in PHP and the way to store it as variables. in this a part of the tutorial i will show you ways to use IF statements to create choices in your scripts.

The if, elseif …else and switch statements area unit used to take decision supported the different condition.

You can use conditional statements in your code to create your choices. PHP supports following four deciding statements −

Best PHP Tutorial For Beginners (IF/Else Statements)
IF/Else Statements

01. if statement – executes some code if one condition is true.

Syntax:

if (something == something else) {
    code to be executed if condition is true;
}

02. if…else statement − use this statement if you would like to execute a set of code once a condition is true and another if the condition isn’t true.
Syntax:

if (something == something else){
   code to be executed if condition is true;
}
else{
   code to be executed if condition is false;
}

03. elseif statement − is employed with the if…else statement to execute a set of code if one in all the many condition is true.

Syntax:

if (something == something else){
code to be executed if condition is true;
}
elseif (something == something else){
code to be executed if condition is true;
}
else {
code to be executed if condition is false;
}

04. switch statement − is employed if you want to pick out one in all many blocks of code to be executed, use the Switch statement. The switch statement is used to avoid long blocks of if..elseif..else code.
Syntax:

switch (expression){
   case L-1:
      code to be executed if expression = L-1;
      break;  
   
   case L-2:
      code to be executed if expression = L-2;
      break;
      default:
   
   code to be executed
   if expression is different 
   from both label1 and label2;
}

Syntax for Example:
Write an if/elseif/else statement inside the editor and make it output anything you wish.

if (something == something else) {

} elseif (something == something else) {

} else {

}

Variables:

The most common use of an IF statement is to compare a variable to another piece of text, a number, or another variable.

For example:

if ($username == "codeexercise")

which would compare the contents of the variable to the text string. if section of code will only be executed if the variable is exactly the same as the contents of the quotation marks so if the variable contained ‘codeexercise‘ or ‘CODEEXERCISE‘ it will be false.

Constructing The if Statement

To add to your script, you can now add a if statement:

if ($username == "codeexercise") {
echo "Please enter your password below";
}

This will only display this text if the username is codeexercise. If not, nothing will be displayed. You can actually leave an IF statement like this, as there is no actual requirement to have an ELSE part. This is especially useful if you are using multiple IF statements.

Constructing The ELSE Statement

Adding The ELSE statement is as easy as the THEN statement. Just add some extra code:

if ($username == "codeexercise") {
echo "Please enter your username below";
} else {
echo "We are sorry but you are not a recognized user";
}

Of course, you are not limited to just one line of code. You can add any PHP commands in between the curly brackets. You can even include other IF statements (nested statements).

Other Comparisons

There are other ways you can use your IF statement to compare values. Firstly, you can compare two different variables to see if their values match e.g.

if ($enteredpass == $password)

You can also use the standard comparison symbols to check to see if one variable is greater than or less than another:

if ($age < "13")

Or :

if ($date > $finished)

You can also check for multiple tests in one IF statement. For instance, if you have a form and you want to check if any of the fields were left blank you could use:

if ($name == "" || $email == "" || $password == "") {
 echo "Please fill in all the fields";
 }

Constructing The switch statement:
A switch statement is similar to an if / elseif / else statement in that you can check multiple conditions. Here’s what it looks like:

$myNum = 2;

switch ($myNum) {
 case 1:
 echo "1";
 break;
 case 2:
 echo "2";
 break;
 case 3:
 echo "3";
 break;
 default:
 echo "None of the above";
}
  • A switch statement is made up of the switch keyword, a variable to check, and a pair of curly braces { }. Here we check the value of $myNum.
  • Then we have a case block for each comparison. For example case 1: echo “1”; break; checks whether $myNum is equal to 1. If yes, it echos “1”, and uses break to exit the switch statement.
  • Otherwise, the next case block runs.
  • If all cases return false, the default case gets executed.