SitePoint Sponsor

User Tag List

Results 1 to 2 of 2

Thread: How to pass an

  1. #1
    Confirmed Halfwit
    Join Date
    Oct 1999
    Location
    Vancouver, BC, Canada
    Posts
    983
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Uh.. this is gonna sound stupid, but I can't figure out how to set a function to accept a "conditional" parameter as input.

    For example, in this function below, I am passing in three parameters.. but the third one ($arg3) is optional. Here's how I thought it would work.. but it doesn't.

    Code:
    function display_list($arg1,$arg2,$arg3) {
    
         Code goes here...
    
         if (isset($arg3){$length = $arg3;}
         else {$length = count($lines);}
    
         for($i=0 ; $i < $length ; $i++) {
             code goes here...
             }
    }
    So basically, argument three would be used to pass in the number of times I want to cycle through the for loop.

    Any idea's how to make this work?

    Thanks!
    - A simple online WYSIWYG editor for HTML code snippets.
    - Managed Web Hosting - $3.95/month (resellers welcome)
    - Why pay more? $8.95 domains & $9.95 SSL certificates!

  2. #2
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here is how you can have an arg optional, simply put arg = "" to make it optional or make it equal to something for a default if it is not passed like
    $arg3 = "TEST" If $arg3 is not passed then it will default to TEST

    function display_list($arg1,$arg2,$arg3 = "") {

    Code goes here...

    if ($arg3 != ""){
    $length = $arg3;
    }
    else {$length = count($lines);}
    for($i=0 ; $i < $length ; $i++) {
    code goes here...
    }
    }
    Please don't PM me with questions.
    Use the forums, that is what they are here for.


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
  •