What is IF in PHP? IF in PHP is use for creating a condition. Very often when you write code, you want to perform different actions for different decisions. In creating a web application it really consist whether arithmetic operations or a condition. Every time you can use conditional statements in web programming. Conditional statement is very important in dealing with some important and complex problem in programming especially when it comes to situation. Example of this is that is in Payment. What if you employer would ask you to create a program that would display all the names that has a salary that is greater than with 10,o00 per month? So, how are you going to do this?. In this kind of situation conditional statement gives you the very best solution. You can create a condition in which the program will going to return all the names of the employees that has a salary of greater than 10,000 in one month. Below is a sample illustration on how to do conditioning.
If condition
<?php
If(condition)
{
Statement goes here.......
}
Else
{
Statement goes here.......
}
?>
If Else If Condition
<?php
If(condition)
{
Statement goes here.......
}
Elseif(optional condition)
{
Statement goes here.......
}
Else
{
Statement goes here.......
}
?>
Switch Statement
<?php
Switch(variable)
{
Case “value”:
Echo “print something”;
Break;
}
?>
As you can observe above there are 3 different type of condition. If condition use when you want to execute a set of code when a condition is true and another if the condition is not true. If else statement is used to execute a set of code if one of several condition are true and switch statement used to avoid long blocks of if..elseif..else code. But when you were going to use switch statement you have to be careful with the syntax. The open and close bracket, colon, semicolon and even the quotes, because it might be the reason why when you execute your program it would return an error. Below given the sample codes that illustrates on how to use conditional statement in PHP programming.
If Condition
<?php
If($day==”Momday”)
{
Echo “Today is Monday”;
}
?>
If else Condition
<?php
If($day== ”Momday”)
{
Echo “Today is Monday”;
}
Elseif($day== “Tuesday”)
{
Echo “Today is Tuesday”;
}
Elseif($day== “Wednesday”)
{
Echo “Today is Wednesday”;
}
Elseif($day== “Thursday”)
{
Echo “Today is Thursday”;
}
Else
{
Echo “What day is today?”;
}
?>
Switch Condtion
<?php
Switch($day)
{
Case “Monday”:
Echo “Today is Monday”;
Break;
Case “Tuesday”:
Echo “Today is Tuesday”;
Break;
Default:
Echo “What day is today?”;
}
?>
If there is some problem or questions regarding with this article, just CLICK THIS LINK to be redirected to the forum page. THANK YOU.
Tags: PHP
Yeah that’s what I’m tlankig about baby–nice work!