Full Guideline about If and Else Function

-
Unknown

PHP If Else
This function is just to clarify in php that if something exist then what to show and if its not exist at all then what should be there for user to see. If and Else functions are most commonly used php element next to print or include. This function can work both positively and negatively depending upon your choice hoe to use it.

It generally used where a condition is set for server depending on user data or your data. Most easiest example to explain this is given below -

$date = date("D");
if ($date == "Fri")
 { echo "Have a nice weekend!"; }
else 
 { echo "Have a nice day!"; }

Now in above Example $date is getting the present day and if today is Friday then it executes command "Have a nice weekend!" otherwise it executes "Have a nice day!"if it is any other day. It is positive conditon for if and else.

Now You can use it in negativel condition also in your document. So see below example also


$date = date("D");
if ($date !== "Sunday")
 { echo "Its not Sunday today!"; }
else 
 { echo "Have a nice Sunday!"; }


Here (' !== ') sign in condition is representing negative condition for Sunday. Means on setting !== "Sunday" , you are telling that if condition is set if its not Sunday.

In other cases like Security Case or giving access to some of your closed members than you can set password or username to get acess to any page. Example is given below -

<form method="post"><input name="pass" type="password" /><input type="Submit" /></form>
<?php
$pass = $_POST['pass'];
if ($pass == "abc")
 {
      print 'Logged Successfully';
 }
else
 {
      print 'Error in Log in. Wrong Password Entered.';
 }
?>


So in above example if user enters pass "abc" only the he/she will get access to your page whose link can be added in print of If here. In same way you can ask for username also. But this protection can be used up to some cases or less serious cases.

Last function to discuss here is elseif function. Else if mean that in case that If is not valid for a case than to have a second case with you, you can us elseif. It is also as simple as if itself. So lookout at this simple example -

$d=date("D");
if ($d=="Fri")
{
echo "Have a nice weekend!";
}
elseif ($d=="Sun")
{
echo "Have a nice Sunday!";
}
else
{
echo "Have a nice day!";
}


Here you can see that if its Friday then message comes "Have a nice weekend!", but else if its Sunday then message comes "Have a nice Sunday!". But if its neither Friday nor Sunday today then the message comes "Have a nice Day!".

If you have any doubts feel free to comment below.

Leave a Reply