PHP Logic

Welcome to lesson 2 of this PHP lesson series. In this lesson
we will be covering basic logic and decision making structures of PHP.
Initially, this won’t make any sense, because we’re giving it the
variables it’ll use to make decisions, in the next full lesson, we’ll
actually have user input to look at, and that is where decision making
becomes very important, when acting on information provided by a user.

The if Statement and Comparisons

One of the main decision making statements in PHP is the if
statement. It will take a statement you give it, figured out whether or
not it’s true or false, and then execute or skip a block of code.

If example:

<?php
$var1 = 90;
$var2 = 10;

if($var1 > $var2)
{    
   //This code will only be executed if the above statement is true.
   print "Hi!< br >";
}
   
//This will be executed regardless.
print "Goodbye!"; ?>

This is the most basic use of the if statement, for more
advanced functionality, let’s try using else and else if. else and
elseif allow you to specify what to do if a statement is false, or
matches another statement. The usage is pretty much the same
elseif(statement){ block} and else{ block }.

<?php
$var1 = 90;
$var2 = 10;

if($var1 > $var2)
{
    //This code will only be executed if the above statement is true.
    print "Hi! Variable 1 is greater than variable 2! < br >";
}
elseif($var1 == $var2)
{
    //This code will execute if you set $var1 and $var2 to equal each other.
    print "Hi! variable 1 and variable 2 are equal! ";
}
else
{
    //This code will execute if you make $var1 less than $var2.
    print "Variable 1 is less than variable 2. ";
}

//This will be executed regardless.
print "Goodbye!";
?>

The example directly above will output (when var1 and var2 are not
changed):
Hi! Variable 1 is greater than variable 2!
Goodbye!

Comparison Operators
Symbol English meaning
== Equal to
=== Strict – Equal to
!= Not Equal to
!== Strict – Equal to
>= Greater than or equal to
<= Less than or equal to
< Less than
> Greater than

The switch statement

Writing a whole bunch of if statements can be really tedious,
so if you have a lot of different possible situations, using the switch
statement may be easier. switch(variable){ case: block}

<?php
$var1 = 'car';
switch($var1)
{
   case 24:
      print "Variable 1 is equal to 24!";
      break; //This makes it so the next thing
             //isn't executed.
   case 'car':
      print "Cars are nice";
      break;
   case 'monkey':   //Since there isn't a "break;" between 
      print "...";   //this case and the next one. the next case
                    //will be executed.
   case 'doom':
      print "Monkeys and doom will destroy us all.";
      break;
   default: //Default is a special case that exists when
            //none of the cases are true.
      print "var1 isn't 24, car, monkey, or doom.";
}
?>

As it is now, when the script is run, it will output “Cars are
nice”, however, if you change it to “monkey”, it will say: Monkeys and
doom will destroy us all.

Logically combining statements

There will always be times when you need to do more than one
comparison at once for a given piece of code. One could “nest” if
statements in each other like this:

if(statement1)
{
   if(statement2)
   {
       if(statement3)
       {
           //etc.
       }
   }
}
//This code is not meant to run as it is, don't try :)

An easier way would be to combine statements using “logical
operators” symbols that control how PHP looks at statements and logic.
&& (and) and || (or) will probably be the ones used the
most in code.

Before we continue, it may be a good idea to know what all the
logic symbols are in PHP. Here’s a list of them:

Symbol English Equivalent
&& And
|| Or
! Not
xor Either or
and And
or Or

Usually, you would use one of these operators as they are
called to compare two different statements that are either true or
false. The only exception being the not (!) operator, it is used on one
statement to flip it’s value from true to false or from false to true.

<?php
$var1 = 35;
$var2 = 30;

//&& - And compares two statements, if both are true,
//then the statement evaluates to true, otherwise
//it'll be false.

if(($var1 > $var2) && ($var2 < 60)) //If is used to make decisions.
{
   print "True! Var1 is greater than var2 and var2 is less than 60"; 
}
else
{
   print "False!";
}

//|| - And compares two statements, if one (or both) is true,
//then the statement evaluates to true, otherwise
//it'll be false.

if(($var1 > $var2) || ($var2 >= 60)
{
   print "....True! One of the two statements is true!"; 
}
else
{
    print "....False! Both are false,".
      " var2 is less than 60 and var1 is less than var2";
}
?>

This code will output the following response: True! Var1 is
greater than var2 and var2 is less than 60….True! One of the two
statements is true! If you’ve never scene an if statement used before
in programming, that may look a little weird, but somehow it works
rather well when used right.

The next lesson isn’t even a lesson at all, it’ll be one big
example about how to accept and use user input from a form.

Leave a Reply

  • (will not be published)

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>