• home
  • forum
  • my
  • kt
  • download
  • Shorthand IF/ELSE

    Author: 2008-08-27 08:17:52 From:

    You may or may not have heard of shorthand if/else statements, but they do exist, and trust me they make things SO much more ordered in your code when used in the right way.

    This is the Shorthand version - below this, if you cannot work it out for yourself is a nice description.

        // Will print out ‘i am male’ if $myGender is ‘male’ and vice-versa.
        echo ($myGender == ‘male’ ? ‘i am male’ : ‘I am female’);

    Description of how this actually works
    We all (i would hope) know the normal way of writing if/else statements (here is a recap just incase)

    $myVar = true;
    if($myVar) {
        echo ‘My Var is TRUE!’;
    } else {
        echo ‘My Var is FALSE’;
    }

    That sort of statement is great for in the body of your code, but when want a dynamic selector within your HTML code, it gets very messy.

    EG:

    if($myGender == ‘male’) {
        // Gender is MALE.
        $maleOpt = ’selected’;
        $femaleOpt = ;
    } else {
        // Gender is FEMALE.
        $maleOpt = ;
        $femaleOpt = ’selected’;
    }
    <select name="gender"><
    option value="female" <?= $femaleOpt; ?>>Female</option>
    <option value="male" <?= $maleOpt; ?>>Male</option>
    </select>
     

    Now - that is alot to write and will make your HTML form look a total mess, this is where the shorthand comes in. It allows us to write the same thing inline

    Look:

    <select name="gender">
    <option value="female" <?= ($myGender == ‘female’ ? ’selected’ : ”); ?>>Female</option>
    <option value="male" <?= ($myGender == ‘male’ ? ’selected’ : ”); ?>>Male</option>
    </select>
     

    As you can see - there is a huge difference in the amount of code - and if Wordpress formatted it nicely - it would look loads better!

    And the all important Explanation!

    // This will echo selected to the screen if $myGender is male.
    echo ($myGender = ‘male’ ? ‘ selected’ : );
     

    So - $myGender is the variable we are checking, in this case we are checking to see if $myGender == ‘male’

    The questionmark (?) seperates the ‘answers’.

    The first ‘answer’ is what is set/displayed when the query is true, the second is if the query is false.

    This is the equilivent to :

    if($myGender == ‘male’) {
        echo ’selected’;
    } else {
        echo ;
    }

    Hope that helps some of you out!

    discuss this topic to forum

    relation tutorial

    No relevant information

    Category

      Ad Management (4)
      Calendars (3)
      Chat Systems (7)
      Content Management (6)
      Cookies and Sessions (8)
      Counters (8)
      Database Related (8)
      Date and Time (9)
      Development (15)
      Discussion Boards (8)
      E Commerce (8)
      Email Systems (13)
      Error Handling (7)
      File Manipulation (24)
      Flash and PHP (6)
      Form Processing (19)
      Guestbooks (12)
      Image Manipulation (21)
      Installing PHP (7)
      Introduction to PHP (23)
      Link Indexing (8)
      Mailing List Management (9)
      Miscellaneous (53)
      Networking (8)
      News Publishing (9)
      OOP (24)
      PEAR (6)
      PHP vs Other Languages (2)
      Polls and Voting (6)
      Postcards (1)
      Randomizing (14)
      Redirection (11)
      Searching (9)
      Security (29)
      Site Navigation (16)
      User Authentication (14)
      WAP and WML (7)
      Web Fetching (8)
      Web Traffic Analysis (15)
      XML and PHP (16)

    New

    Hot