Saturday, December 8, 2012

PHP IF Statement - Advanced PHP For Beginners Lesson 6

The IF statement
By Stephon Unomon

 

       The IF statement in PHP is very similar to using IF in real life. Like IF you don’t set your alarm clock, then you’ll be late to work in the morning. IF (and it’s friend ELSE) are known as “conditionals”.

 

       First off, let’s look at how PHP compares values for conditionals. You’ll see “operators” in any IF statement:

 

           ==        Equal to

           !=         Not equal to

           <          Less than

           >          Greater than

           <=        Less than or equal to

           >=        Greater than or equal to

 

So, a valid IF statement could be illustrated as follows:

 

<?php

if ($variable == "some value") {

echo "Correct";}

?>

Expanding on IF with ELSE

 

       You’ll most likely want to use IF with ELSE. ELSE gives you the option of doing something ELSE with your PHP script if IF doesn’t calculate one way or another. For example, you can have your site display something IF a condition is met (like a password was correct...see below!) or something ELSE if not. (Like a redirect if the password is not!)

 

 

 

 

Code example...

 

<?php

$five = “5”;

if ($five == “5”) {

    echo "You are correct";

} else {

    echo "You are incorrect";

}

?>

Real World Usage For If/Else

 

       You could create a simple password protected area using If/Else. A PHP page with a conditional statement could be set up to process an HTML login form. A variable “$password” could be set, and the Header command could be used to redirect on success or failure.

 

Your HTML page would be a simple form...

 

<form action="login.php" method="post">

<INPUT type="password" name="password">

<input type="submit">

</form>

 

This HTML form will set the variable in the “action” page (named login.php in this example) with the $_POST command (more on this later) and do one of two things: If the password is correct, it will show the desired content. If it is NOT correct, it will redirect to another URL (or page). Since the action is in PHP, viewing the source in the web browser isn’t going to reveal the password.

 

 

...And your PHP “Action” page would be an If/Else combined with a Redirect...

 

<?php

if($_POST['password'] == 'some_password'){

 

echo “

<!---Put your protected HTML content here...-->

“ ;

} else {

header ("location: some_error_page.html");

}

?>

 

...And Voila! A simple way to password protect a page. I wouldn’t use this for sensitive stuff (like putting your social security number online) but it’s good for a simple, single layer of security.

 

IF, meet ELSE. ELSE, meet ELSEIF

 

       The IF/ELSE statement is wonderful if you need to check for only one condition. But, what if you need to check for multiple conditions? Like, for instance, IF a truck is a Dodge, do this...ELSE a truck is a Chevy, do this...but what if you need to have options if a truck was a Ford?

In this example, we simply want to see if a truck is a Dodge or not. We can do this with IF / ELSE...

 

<php

$truck = "Chevy";

if($truck == "Dodge"){

          echo "It’s Ram Tough!";

} else {

          echo "We’ll Be There!";

}

?>

 

Now, if we wanted to see if the truck was a Ford, we’d add the ElseIf statement...

<php
$truck = "Chevy";
if($truck == "Dodge"){
          echo "It’s Ram Tough!";
} elseif {$truck == “Ford”}
          echo “Built Ford Tough!”;
} else {
          echo "We’ll Be There!";
}
?>
 
...And so on. You could continue to use ElseIf to declare other Trucks. One thing to remember about ElseIf is that it can’t be used without IF. So what if you have a lot of ElseIf’s?? Let’s see what’s behind the curtain, Bob...
 
Flip the SWITCH
 
       Sometimes we have to evaluate more than just a few cases, making ElseIf a tad cumbersome (do YOU want to write 20 ElseIf’s? I don’t!) Enter the more streamlined and efficient SWITCH command.Let’s add some more trucks to our list, shall we?
 
<?php
$truck = "Chevy";
echo "Drive a $truck, <br/>";
switch ($truck){
          case "Dodge":
                   echo "Ram Tough!";
                   break;        
          case "Ford":
                   echo "Built Ford Tough!";
                   break;        
          case "Toyota":
                   echo "Got The Guts?";
                   break;        
          case "Nissan":
                   echo "Shift_power";
                   break;        
          case "GMC":
                    echo "Professional Grade";
                   break;        
} ?>
That looks a little cleaner, don’t you think? A tad less clumsy that an equal number of If/Else statements. Make sure when you use Switch to include the “break” statement - it not, the information will be processed until the script “breaks” or ends.
 
Also, notice that there is no default statement for when we match our condition! We need to add something to Switch - the default case.
 
<?php
$truck = "Chevy";
echo "Drive a $truck, <br/>";
switch ($truck){
          case "Dodge":
                   echo "Ram Tough!";
                   break;        
          case "Ford":
                   echo "Built Ford Tough!";
                   break;        
          case "Toyota":
                   echo "Got The Guts?";
                   break;        
          case "Nissan":
                   echo "Shift_power";
                   break;        
          case "GMC":
                   echo "Professional Grade";
                   break;
          default:
                   echo "We’ll Be There!";
                   break;                  
} ?>
 
This way, if there are no matching cases, our default is displayed.
 
 




 

 

No comments:

Post a Comment