Categories:

    Lesson 7 - Control Structures

    IF

    Today we will go over simple control structures. You can think of them as "forks in the road" of your code. Depending 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 (code = 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" (Which it is! :P )
    print "2 is bigger than 1";
    }
    ?>

    However, the following "if" statement 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";
    }
    ?>
    Quote:
    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 [the] C [coding language]. - PHP.net
    if (expression = true) {
    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". Now write the following text in the file:

    /* Make a new variable called "$cpu_number"
    and set it equal to a random number between 0 and 100. */
    $cpu_number = rand(0, 100);

    // Make a new variable called "$your_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! CPU number is $cpu_number";
    }

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

    When you run the code, you will ether see "Your Number Is Bigger!" or "Your Number Is Smaller", depending on wither the computer chose a number bigger or smaller than yours. Run the script several more times to make different outcomes and you can also change the code to say different things! If you want you can add a third "if" that says something if both numbers are the same (If ($your_number == $cpu_number)).

    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 following:


    Your Number:




    /* Make a new variable called "$cpu_number"
    and set it equal to a random number between 0 and 100. */
    $cpu_number = rand(0, 100);

    /* "if" something was posted (i.e. isset in 'num') 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! CPU number is $cpu_number";
    }

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

    }

    ?>

    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 chooses.

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

    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 statement is TRUE PHP will go on to process the next two "if" statements. If the first "if" statement 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 - the first if is FALSE until you post a number!

    Not Equal Too?

    It sounds like someone needs speech lessons! Actually, that is how this next "operator" reads.

    if ($variable is NOT EQUAL to $variable2) {
    then do statement
    }
    ?>

    Open backup up you text editor and make a new file called "lesson7.php". Then type the following into it:

    // set "$cpu_number" to a random number between 0 and 3
    $cpu_number = rand(0, 3);
    // Set $your_number equal to "1"
    $your_number = 1;

    If (
    $your_number != $cpu_number) {
    /* "if" $your_number is not equal to $cpu_number do the following: */
    echo "You have a different NUMBER! CPU number is $cpu_number";
    }

    If (
    $your_number == $cpu_number) {
    /* "if" $your_number is equal to $cpu_number do the following: */
    echo "Your number is the same as the CPU's number!";
    }
    ?>

    Run the page a couple of times to see the output of both if statements.

    Other PHP Operators

    PHP actually has lots of operators besides ==, !=, . Here are some of them:

    if ($variable is less than or equal to $variable2) {
    then do statement
    }
    if (
    $variable <= $variable2) {
    then do statement
    }
    ?>
    if ($variable OR $variable2 is equal to $variable3) {
    then do statement
    }
    if (
    $variable || $variable2 == $variable3) {
    then do statement
    }
    ?>

    Note: In an OR expression if either of the variables are true, the statement is true - or if both are true, the statement is true! However, if you only want the statement to be true if BOTH are true, then you need to use the AND (&&) operator:

    if ( ($variable is equal to $variable3) AND ($variable2 is equal to $variable3) ) {
    then do statement
    }
    if ( (
    $variable == $variable3) && ($variable2 == $variable3) ) {
    then do statement
    }
    ?>

    Don't worry about memorizing these operators right now. As we use them in future scripts you will get the hang of them.

    0 Responses