Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 1 of 1
  1. #1
    Member Cranavvo's Avatar
    Join Date
    Sep 2013
    Location
    St. Helens, England
    Posts
    70
    Member #
    37189
    Liked
    11 times

    PHP Tutorial 7 - If/Else statement


    This is a follow-up to my previous tutorial: PHP Tutorial 6 - Including/Requiring. It's wise you should read the last tutorial before you come onto the next one.

    Yet again, If/Else is used across all programming languages. It works by you typing in a statement to that returns a true or false value (boolean value).

    There's two parts of this which you need to understand. When you're only seeking for a value of "true" then you simply:

    PHP Code:
    <?PHP
    $truth 
    true;
    if(
    $truth){
    echo 
    "The value is true.";
    }else{
    echo 
    "The value is false.";
    }
    ?>

    The value is true.
    However if you looking for if the value isn't true:

    PHP Code:
    <?PHP
    $truth 
    true;
    if(!
    $truth){
    echo 
    "The value isn't true.";
    }else{
    echo 
    "The value is true.";
    }
    ?>

    The value is true.
    The "!" means "is not" and comes up a lot in PHP. Another thing you'll see in PHP is "&&" and "||" these are short-hands to writing "AND" and "OR", it's upto you which you decide to use in this next part:

    PHP Code:
    <?PHP
    $yourname 
    "Connor"
    $allowed true;
    if(
    $yourname == "Connor" AND $allowed){
    echo 
    "Welcome Connor!";
    }else{
    echo 
    "Connor's not allowed in!";
    }
    ?>

    Welcome Connor!
    Pretty easy! If statements are so reliable in PHP and you'll be using lots of them in time to come. There's also a shorthand to if statements which can make it easier to concatenate into strings:

    PHP Code:
    <?PHP
    $truth 
    true;
    $string "The statement is " . (($truth) ? "true!" "false!" );
    echo 
    $string;
    ?>

    The statement is true!
    Easy as that. Thanks for reading!

  2.  


Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 01:23 PM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com