Capable SEO

If Then Math PHP Statements

  1. Lets get started by opening your favorite editor and starting a fresh source page. Start off with the basic coding which denotes a php area. <?php ?>
  2. Now lets add in some basic variables and an echo statement. Give two variables a value of 2, and a third variable a value equal to the previous to variables added together.
    <?php
    $x
    =2;
    $y=2;
    $number=$x+$y;
    echo
    $number;
    ?>

    This should print out 4 .

  3. Try changing the + to different mathmatical operators these are:
    / Divide
    * Multiply
    - Subtract
  4. Now we’re going to add in an if then statement. Basically if statements are logical, if certain requirements are met then do something. An if then statement looks like this
    <?php
    if (/*requirements*/)
    {
    //Stuff to do
    }
    ?>

    The if statement is one of the few that doesn’t require a ; after it.

  5. Now lets a simple statement that says, ‘if’ ‘$number is not equal to four’ ‘echo Thats not Four’. This code looks like this.
    <?php
    $x
    =1;
    $y=2;
    $number=$x+$y;

    if ($number!=4)
    {
    echo
    "Thats not Four";
    }
    ?>

    This should print out “Thats not Four

    The comparison operators in PHP are as follows:
    != Is not equal to
    == Equal to
    > Greater than
    < Less than
    >= Greater than or Equal to
    <= Greater than or Equal to

  6. Now lets say you want to check if the number is between 5 and 10. You’ll need to use two checks in the if instead of one. So you need to say ‘if’ ‘$number is greater than or equal to 5 and less than or equal to 10′ ‘echo number is between 5 and 10′. The code for this looks like this:
    <?php
    $x
    =1;
    $y=2;
    $number=$x+$y;

    if ($number>=5 && $number<=10)
    {
    echo
    "number is between 5 and 10";
    }
    ?>

    This shouldn’t print anything

    Logical Operators for PHP
    && and
    || or

  7. Now this is fine and all except what, happens when your number doesn’t fit the criteria? Nothing happens. So lets add on the final part of an If Then statement. So now PHP will go if $number is between 5-10 do this, or else do this. The code looks like this:
    <?php
    $x
    =1;
    $y=2;
    $number=$x+$y;

    if ($number>=5 && $number<=10)
    {
    echo
    "number is between 5 and 10";
    }
    else
    {
    echo
    "number is less then 5 or greater than 10";
    }
    ?>

    This should print number is less then 5 or greater than 10

Capable SEO