Function definition with optional parameters?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Phester

    Function definition with optional parameters?

    Some of the functions that are part of PHP have optional parameters. Is
    it possible to define your own function so that it has optional
    parameters? If so, how is that done?

    Thanks,

    Phester

    vilefesteringsc um
    at
    yahoo

  • Andy Hassall

    #2
    Re: Function definition with optional parameters?

    On Sun, 28 Sep 2003 20:20:33 GMT, Phester <phester@nonexi stenthost.com> wrote:
    [color=blue]
    >Some of the functions that are part of PHP have optional parameters. Is
    >it possible to define your own function so that it has optional
    >parameters? If so, how is that done?[/color]

    Certainly; you can give the optional parameters default values.



    --
    Andy Hassall (andy@andyh.co. uk) icq(5747695) (http://www.andyh.co.uk)
    Space: disk usage analysis tool (http://www.andyhsoftware.co.uk/space)

    Comment

    • Pedro

      #3
      Re: Function definition with optional parameters?

      Phester wrote:[color=blue]
      > Is
      > it possible to define your own function so that it has optional
      > parameters?[/color]

      Yes.

      [color=blue]
      > If so, how is that done?[/color]

      Define a function, giving the last parameters a default value

      <?php
      function xyz($a, $b, $c=0, $d='localhost') {
      // do nothing :-)
      }

      ### and then call it
      xyz(0, 0, 3, 'yahoo.com');
      xyz(0, 0, 4); // same as xyz(0, 0, 4, 'localhost');
      xyz(0, 0); // same as xyz(0, 0, 0, 'localhost');
      ?>

      --
      I have a spam filter working.
      To mail me include "urkxvq" (with or without the quotes)
      in the subject line, or your mail will be ruthlessly discarded.

      Comment

      • Phester

        #4
        Re: Function definition with optional parameters?

        Andy Hassall wrote:[color=blue]
        > On Sun, 28 Sep 2003 20:20:33 GMT, Phester <phester@nonexi stenthost.com> wrote:
        >[color=green]
        >>Some of the functions that are part of PHP have optional parameters. Is
        >>it possible to define your own function so that it has optional
        >>parameters? If so, how is that done?[/color]
        >
        > Certainly; you can give the optional parameters default values.
        >
        > http://www.php.net/manual/en/functions.arguments.php
        >
        > --
        > Andy Hassall (andy@andyh.co. uk) icq(5747695) (http://www.andyh.co.uk)
        > Space: disk usage analysis tool (http://www.andyhsoftware.co.uk/space)[/color]


        Thanks!

        Phester

        Comment

        • Phester

          #5
          Re: Function definition with optional parameters?

          Pedro wrote:[color=blue]
          > Phester wrote:
          >[color=green]
          >>Is
          >>it possible to define your own function so that it has optional
          >>parameters?[/color][/color]

          ....
          [color=blue]
          > Define a function, giving the last parameters a default value
          >
          > <?php
          > function xyz($a, $b, $c=0, $d='localhost') {
          > // do nothing :-)
          > }
          >
          > ### and then call it
          > xyz(0, 0, 3, 'yahoo.com');
          > xyz(0, 0, 4); // same as xyz(0, 0, 4, 'localhost');
          > xyz(0, 0); // same as xyz(0, 0, 0, 'localhost');
          > ?>[/color]


          Thanks, that's exactly what's required.

          Phester

          Comment

          Working...