Jump to content

  •  

  • iBotModz CBox


    Photo

    Beginning PHP - Lesson 7 - Control Structures


    This topic has been archived. This means that you cannot reply to this topic.
    No replies to this topic

    #1 fattwam

    fattwam

      General Grade 2

    • Sub-Admins

    • 3,979 posts
    • Joined: 18-August 07

    Posted 27 March 2008 - 08:14 PM

    IF

    Today we will go over simple control structures. You can think of them as "forks in the road" of your code. Depinding on whether the control structures are true or false will determine what your code does next. Here is a simple example of a control structure:


     if (expression = true)
    then do statement
    ?>



    "If" checks to see if the expression is TRUE and if it is: the "if" will run the code below it. In other words - "If" an expression evaluates to TRUE, PHP will execute the following statement, and if it evaluates to FALSE - it'll ignore it.


    The following example would display "2 is bigger than 1" (2 > 1) if 2 IS bigger than 1:


     if (2 > 1) // if "2" is greater than (>) "1"
    print "2 is bigger than 1";
    ?>




    However, the following "if" statment would not do anything because it is FALSE:



     
    if (1 > 2) {
    /* (if "1" is greater than "2"). FALSE because 1 is smaller than 2
    */
    print "1 is bigger than 2";
    }
    ?>






    More About "if":

    "Any PHP script is built out of a series of statements. A statement can be an assignment, a function call, a loop, a conditional statement of even a statement that does nothing (an empty statement). Statements usually end with a semicolon. In addition, statements can be grouped into a statement-group by encapsulating a group of statements with curly braces. A statement-group is a statement by itself as well.The if construct is one of the most important features of many languages, PHP included. It allows for conditional execution of code fragments. PHP features an if structure that is similar to that of C:" -PHP.net


     if (expression)
    then do statement
    ?>












    Now lets make a simple script that checks a number to see if it is to big, or if it is to small. Open your text editor (if you don't have one use Notpad) and save the blank file as "if.php" (with the quotes). Now write the following text in the file:




     /* Make a new variable called "$cpu_number" and set it equal to "34". */
    $cpu_number = 34;
    /* Make a new variable called "$number" and set it equal to "50". */
    $your_number = 50

    If ($your_number > $cpu_number) {
    /* "if" $your_number is greater than $cpu_number do the following: */
    echo "Your Number Is Bigger!";
    }

    If ($your_number < $cpu_number) {
    /* "if" $your_number is less than $cpu_number do the following: */
    echo "Your Number Is Smaller!";
    }

    }
    ?>




    When you run the code you will see "Your Number Is Bigger!" on the screen because the first "if" statment equaled TRUE (50 is greater than 34) while the second "if" was FALSE (50 is less than 34), so only the first statment ran.




    Now lets make it so that a user can enter a number and see if they have a bigger number. Change the code in "if.php" to the folowing:





    Your Number:




    /* Make a new variable called "$cpu_number" and set it equal to "34". */
    $cpu_number = 50;

    /* "if" somthing was posted (isset) do the following: */
    if (isset($_GET['num'])) {
    $number = $_GET['num'];
    /*put the posted variable "num" into a new variable called "$number". */

    If ($number > $cpu_number) {
    /* "if" $number is greater than $cpu_number do the following: */
    echo "Your Number Is Bigger!";
    }

    If ($number < $cpu_number) {
    /* "if" $number is less than $cpu_number do the following: */
    echo "Your Number Is Smaller!";
    }

    }

    ?>






    When you save the file and upload it to your server you will be presented with a form where you can enter a number and see if it is higher or lower than the number the cpu chose.


    You will notice in the above example there are two "if" statements inside of another "if" statment:



     if (isset($_GET['num'])) { 
    $number = $_GET['num'];
    If ($number > $cpu_number) {
    echo "Your Number Is Bigger!";
    }
    If ($number < $cpu_number) {
    echo "Your Number Is Smaller!";
    }
    }
    ?>




    If the first statment is TRUE PHP will go on to process the next two "if" statments. If the first "if" statment is FALSE PHP will ignore the other two "if" statements inside of the first. That is why you don't see anything when you first run the script - it equals FALSE untill you post some value!



    For more information please read these articles:


    Using If Else Statements
    Operators
    Looping The Loop
    If and Switch
    Simple Loops


    If you find a broken link please PM me, thanks!