Capable SEO

How to do PHP While Loops

Now that you know how to use if then statements lets move on to a basic loop.

  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 a variable and an echo statement. Give one variable a value of 0 then echo it.
    <?php
    $x
    =0;
    echo
    $x;
    ?>

    This should print out 0.

    PHP generally starts many constructs at 0, not at 1 so its generally better to start counting from 0 in programming..

  3. Now lets add in a basic loop. The while statement is very similar to an if statement except a while loop will do the things as long as its conditions are met (or until timeout which is generally 3-5 minutes depending on your server settings).
    <?php
    while (/*requirements*/)
    {
    //Stuff to do
    }
    ?>

    This won’t print anything yet

  4. Now lets make that actually do something. We’ll have it run 5 times, so while $x<5 do something. Of course we have to increase $x inside the loop otherwise it won’t end. So the only action we have right now is adding one to $x, we can do this with $x=$x+1; or in english x is equal to x +1.
    <?php
    $x
    =0;
    while (
    $x<5)
    {
    $x=$x+1;
    }
    ?>

    This won’t print anything yet

  5. Now lets add on an echo so we can see the progress of $x counting up towards 5.
    <?php
    $x
    =0;
    while (
    $x<5)
    {
    echo
    $x;
    $x=$x+1;
    }
    ?>

    This should print out “12345

  6. Ok now there is a better way to add one to $x and thats a special operation which is coded like this ++$x;
  7. Now lets add some text to each echo so it looks a bit nicer. You can print a variable and letters by using an echo with quotes. so in this statement echo “x: $x”; the first x will simply be the letter x and the second one which is preceded by a $ will be the variable.
    <?php
    $x
    =0;
    while (
    $x<5)
    {
    echo
    "x=$x";
    ++
    $x;
    }
    ?>

    This should print out “x: 1x: 2x: 3x: 4x: 5
    Now this doesn’t look much nicer (actually it looks worse!), but we’ll fix that in the next step.

  8. A PHP echo will place code directly into the html of a file. So if you type html code into the echo you can use different html functions to change displays. In this way you can make things bold, manipulate css styles etc. For now we’ll just add a <br> to the end of each line this will make the numbers go to different lines.
    <?php
    $x
    =0;
    while (
    $x<5)
    {
    echo
    "x=$x<br>";
    ++
    $x;
    }
    ?>

    This should print out:
    x: 1
    x: 2
    x: 3
    x: 4
    x: 5

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