Results 1 to 8 of 8

Thread: Functions...

  1. #1
    Join Date
    Mar 2006
    Location
    Cleveland, Ohio
    Posts
    574
    Thanks
    6
    Thanked 5 Times in 5 Posts

    Default Functions...

    I'm using functions on a website I'm creating.
    I'm curious if we can use if statements, etc in a function?
    For example:

    PHP Code:
    function my_function($yeah) {
       if (...) {....}
       elseif (...) {....}
       else {....}

    the periods obviously represent the content...

    at any rate, is such a thing possible?
    I have statements that repeat themselves, with just one part changing. so I figured if I could just call a function for it, I wouldn't have to keep rewriting the statements?

    Thanks

  2. #2
    Join Date
    Sep 2005
    Posts
    882
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default

    Yes. That is the whole point of functions.

  3. #3
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    What is a function without statements?
    - Mike

  4. #4
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Well... one of them

    A note on elseif: don't use it. It's directly equivalent to a plain old else if, but designed especially for stupid people and lazy typists. Using it is a bad habit, and it doesn't exist in most languages anyway. Its existance in PHP is another indicator of what PHP has sacrificed to become a rapid-development language.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  5. #5
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    Wait a minute... I've used "if (...) {...}" many times over and not once, have I had to use an else if. What's "else if" for?
    - Mike

  6. #6
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    It's to do with braces. When only one statement is conditional, the braces are not necessary; for example,
    Code:
    if($var) $var = false;
    else $othervar = true;
    However, an if block is considered a single statement, so:
    Code:
    if($var1) {
      dog();
      cat();
    } else if($var2) {
      bird();
      fish();
    } else {
      fox();
      rabbit();
    }
    is equivalent to:
    Code:
    if($var1) {
      dog();
      cat();
    } else {
      if($var2) {
        bird();
        fish();
      } else {
        fox();
        rabbit();
      }
    }
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  7. #7
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    Ah, I see. I get it now
    - Mike

  8. #8
    Join Date
    Mar 2006
    Location
    Cleveland, Ohio
    Posts
    574
    Thanks
    6
    Thanked 5 Times in 5 Posts

    Default

    Ok, thanks.

    And from now on I'll use "else if"....
    haha

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •