PHP Tutorial [ 3 ]

Discussion in 'PHP' started by DarkMindZ, Dec 31, 2007.

  1. #1
    Welcome!

    This is The Third PHP Tutorial Of Many from Ac1d..

    You Should Already have Learnt About The Following:
    -= Admin Defined Variables
    -= Echo and Print functions
    -= Predefined variables
    -= IF and ELSE statements
    -= Arrays
    -= Comments
    -= Heredoc

    From Here on in Most of the codes will be explained via comments so dont skip any of the articles ;)

    In This Tutorial I Will Cover:
    -= IF / ELSE statements (extended)
    -= ELSEIF Statements
    -= Different Various Operators

    =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

    I will Start with The Various PHP Operators.
    No, Operators aren't people who answer the phone xP.

    Operators tell something what to do (to a certian extent) they usually compare two or more things.
    - Arithmatic Operators -
    =-=-=-=-=-=-=-=-=-=-=-=-=

    + - Add
    - - Subtract
    * - Multiply
    / - Divide
    % - Modulus (The Devision Remainder)
    ++ - Increment
    -- - Decrement

    Examples:

    1+1=2
    2-1=1
    2*5=10
    10/2=5
    20%7=1
    5++=6
    5--=7


    - Comparison Operators -
    =-=-=-=-=-=-=-=-=-=-=-=-=

    != - Not Equal To
    == - Equal To
    > - Is Greater than
    < - Is Less Than
    >= - Is Greater than or Equal to
    <= - Is Less than or equal to

    Examples:

    1 != 2 returns true
    1 == 2 returns false
    1 > 2 returns false
    1 < 2 returns true
    1 >= 2 returns false
    1 <= 2 returns true



    - Logical Operators -
    =-=-=-=-=-=-=-=-=-=-=-=

    && - AND
    || - OR
    ! - NOT

    Examples:

    (1 < 2 && 2 != 1) returns true
    (1 > 2 || 2 != 1) returns true
    !(1 > 2) Returns true

    Hope you understood that because those 3 parts are vital to PHP.
    Again if you need any help with those PM / Email or IM me.



    Onto IF statements....



    IF and ELSE statements are so vital to php you couldnt create a nice script without them.

    Here is an example of a very common IF and ELSE usage.

    <?php
    $user = $_POST['username'];
    $pass = $_POST['password'];

    // If Our Form Hasnt Been Submitted Show it.
    IF(!$_POST){
    echo "<form method=post>Username: <input type=text name=username><br />Password:<input type=password name=password><br /><input type=submit value=Login></form>";
    // If Our Form HAS been submitted then check the login
    }ELSE{
    // Check if the user input was correct
    IF($user == "test" && $pass == "test"){

    // Show the welcome user text
    echo "Welcome test<br>You Have Successfully Logged In";
    }ELSE{
    // If Userinput was incorrect show Invalid Login text
    echo "Wrong Username And/Or Password!<br>Please Try Again By Clicking <a href=index.php>here</a>";
    }
    }
    ?>


    Hopefully you understood that simple script.
    It checked a user's input to see if the input field named 'username' was test and the input field named "password" was test.
    If that statement was true, it displayed the 'Welcome' Text.
    If that statement was False, it Displayed the 'Invalid Login' Text.

    Now to add an ELSEIF


    ELSEIF is a function to be used within an if statement like ELSE, only syntax rules are that it comes AFTER the if statement and BEFORE the 'ELSE' Statement (if any)..

    it adds another option so if the 'IF' statement returned false, the 'ELSEIF' would be used next, Otherwise ignored.

    Example:

    <?php
    $user = $_POST['username'];
    $pass = $_POST['password'];

    // If Our Form Hasnt Been Submitted Show it.
    IF(!$_POST){
    echo "<form method=post>Username: <input type=text name=username><br />Password:<input type=password name=password><br /><input type=submit value=Login></form>";

    // If Our Form HAS been submitted then check the login
    }ELSE{

    // Check if the user input was correct
    IF($user == "test" && $pass == "test"){

    // Show the welcome user text
    echo "Welcome test<br>You Have Successfully Logged In";

    }ELSEIF($user == "admin" && $pass == "letmein"){

    // Show the welcome ADMIN welcome text
    echo "Welcome Admin<br>You Have Successfully Logged In as An Administrator";

    }ELSE{
    // If Userinput was incorrect show Invalid Login text
    echo "Wrong Username And/Or Password!<br>Please Try Again By Clicking <a href=index.php>here</a>";
    }
    }
    ?>

    as you can see, still simple.

    Well that sums up all for this tutorial, but keep an eye out for number 4 and maybe even more!



    Nusquam Est Impossible
    Ac1d
     
    DarkMindZ, Dec 31, 2007 IP