Jump to content

  •  

  • iBotModz CBox


    Photo

    Beginning PHP - Lesson 10 - Review


    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:18 PM

    I would like to use this lesson to go back over what we have learned about variables, strings, and control structures. If you feel you have mastered these then you are welcome to skip this lesson.


    So once again, a variable can be given any of the following values, any of the following ways:

     //Numbers:
    $var1 = "993"; //$var1 is equal to 993
    $var2 = 993; //$var2 is equal to 993
    $var3 = '993'; //$var3 is equal to 993

    //Letters:
    $var4 = "G";
    $var5 = 'G';

    //Characters:
    $var6 = '%';
    $var7 = "+";

    //Variables:
    $var8 = '8';

    $var9 = $var8;
    //Variable 9 ($var9) is now equal to "8"

    $var9++;
    $var10 = $var9;
    //Variable 10 is now equal to "9" ("8" plus one, or "++")

    $var11 = $var10 + $var8;
    //Variable 11 is now equal to 17

    $var12 = ((0.1+0.7) * 10); // $var12 is equal to 8
    $var13 = '((0.1+0.7) * 10)'; // $var12 is equal to "((0. 1+0.7)*10)"!
    //Remember the '' means ABSOLUTE VALUE, while "" means PHP evaluates it.

    ?>



    A variable can hold a letter or any number.



    Strings hold long "strings" of letters or numbers - like as a sentence. All of the following are strings:


    Sentences:
     $string1 = "Welcome to Learnphpfree.com";
    $string2 = "5448 Longroad, Somewhere, CA 99999";
    $string3 = "I love programming!?";

    $string4 = "Learn";
    $string5 = " PHP";
    $string6 = " Free!";
    $string7 = "$string4 $string5 $string6";
    //String 7 is "Learn PHP Free!"

    $string8 = "$var8 visitors to Learnphpfree.com";
    //String 8 is equal to "8 visitors to Learnphpfree.com"

    $string9 = $var11. " visitors to Learnphpfree.com". ' Since yesterday';
    //String 9 is equal to "17 visitors to Learnphpfree.com Since yesterday"
    ?>


    You will be doing a lot with strings so make sure and practice with them on your own so that you know them well.


    Control Structures are the logic of the code. Remember that if the "if" is FALSE than PHP while run the "else" statement, but if the "if" is true PHP will ignore the "else" statement.

    Remember that these characters dictate the control structure:

    ">" means "Greater than"
    "<" means "less than"
    "==" means "value is equal"
    "=" means "make equal"
    "&&" means "and"
    "||" means "or"
    "+" means "plus"
    "-" means "minus"
    "!" means "NOT"
    "!=" means "NOT equal"
    ">+" means "Greater than or equal to"


    Here are a couple simple ones:

     if (1 > 0) {
    echo "1 is greater than 0";
    } else {
    echo "1 is not greater than 0";
    }
    //the "if" is TRUE because 1 is greater than 0



    if (1 == 0) {
    echo "1 is equal to 0";
    } else {
    echo "1 is not equal to 0";
    }
    //the "if" is FALSE because 1 is NOT equal to 0



    if (1 != 0) {
    echo "1 is not equal to 0";
    } else {
    echo "1 is equal to 0";
    }
    //the "if" is TRUE because 1 is NOT equal to 0



    if (1 >= 0) {
    echo "1 is greater than or equal to 0";
    } else {
    echo "1 is not greater than or equal to 0";
    }
    //the "if" is TRUE because 1 is greater or equal to 0
    ?>



    ADVANCED LOOPS:

    For "&&" both have to be TRUE for the "if" to be true.
    For "||" only one has to be TRUE for the "if" to be true.



     if ((1 > 0) && (2 > 0)) {
    echo "1 is greater than 0 AND 2 is greater than 0";
    } else {
    echo "1 is not greater than 0 AND/OR 2 is not greater than 0";
    }
    //the "if" is TRUE because BOTH are greater than 0



    if ((1 > 0) && (2 > 0)) {
    echo "1 is greater than 0 AND 2 is greater than 0";
    } else {
    echo "1 is not greater than 0 AND/OR 2 is not greater than 0";
    }
    //the "if" is TRUE because BOTH are greater than 0



    if ((1 > 0) || (2 == 0)) {
    echo "1 is greater than 0 OR 2 is equal to 0";
    } else {
    echo "1 is not greater than 0 OR 2 is not equal to 0";
    }
    //the "if" is TRUE because the first statement is TRUE even though the second is FALSE.



    if ((1 < 0) || (2 >= 0)) {
    echo "1 is less than 0 OR 2 is greater than or equal to 0";
    } else {
    echo "1 is not less than 0 OR 2 is not greater than or equal to 0";
    }
    //the "if" is TRUE because the first statement is FALSE but the second is TRUE.

    ?>



    Last I would like YOU to try to make your own "if" statements and try making and messing with strings and variables too! ]Here is a zip archive of the loops, variables, and strings files. Upload them to your own site and experiment changing them. You need to make sure you understand them well.