Jump to content

Passing optional values to a function


HuggieBear

Recommended Posts

I have the following function which prints a form, now as you can see, it accepts an argument, the argument is the location that the form submits to.  Now if I call the function with an argument it works fine, however, if I leave it blank I get an error.

The code is:
[code]<?php
  function drawForm($location){
      $location = isset($location) ? $location : $_SERVER['PHP_SELF'];
      $out = <<<HTML
      <form name="login" id="login" method="post" action="$location">
        <!-- My form code here -->
      </form>
HTML;
  return $out;
}
?>[/code]

The error is:
[quote]Warning: Missing argument 1 for drawForm(), called in ....[/quote]

I know the error is coming and I know why, but is there a way to get around it.  I can suppress the error by calling [code=php:0]echo @drawForm();[/code] but I'm not keen in that really.  How can I make an argument optional?

Regards
Huggie
Link to comment
Share on other sites

There's another more flexible method of passing arguments, which avoids a lot of the hassle caused by ordering of optional arguments.

[code]function foo($args) {
  if (array_key_exists('dbh', $args)) $dbh = $args['dbh'];
  if (array_key_exists('result', $args)) $result = &$args['result']; # pass by reference
}[/code]

This allows you to set as many arguments as you want.  It's quite popular in perl programming.

Note that array_key_exists() allows null valued arguments, whereas isset() will not allow passing of null values.

Edit: No it's me who's tired :)  Now THAT'S pass by reference..
Link to comment
Share on other sites

Like

[code]$result = foo(array(
  'arg' => $arg,
));[/code]

For functions which only ever take one argument I usually don't use arrays.  But once the number of arguments start growing, and particularly when there are optional arguments, I switch the function over to using array arguments.  It makes life so much easier for functions like this:

[code]$result = foo(array(
  'dbh' => $dbh,
  'term' => $term,
  'distance' => 3,
  'wibble_tolerance' => 0.3,
  'globify' => true,
  'puppydogs' => 'cute',
));[/code]
Link to comment
Share on other sites

Any and all arguments are totally optional in this example and do NOT need to be passed as an array:
[code]
function test(){
$args =func_get_args();
foreach($args as $arg){
    echo "<br />" . $arg;
  }
}
[/code]
see http://us2.php.net/manual/en/function.func-get-args.php
Link to comment
Share on other sites

[code]<?php
function foo($args) {
  if (array_key_exists('dbh', $args)) $dbh = $args['dbh'];
  if (array_key_exists('result', $args)) $result = &$result; # pass by reference
}
?>[/code]
Perhaps it's because I'm just tired, but how is that pass by reference?
Link to comment
Share on other sites

[quote author=doni49 link=topic=114338.msg465246#msg465246 date=1163041187]
...
see http://us2.php.net/manual/en/function.func-get-args.php
[/quote]

Unfortunately that has the same limitations as optional arguments to functions.. how do you tell the function that you want to set 'foo' to true, but don't want to set 'baz' to any value?

func_get_args() implements variable length argument lists, but doesn't implement named arguments.

Roopurt18, well spotted, I've edited that post :)  Of course you must also use '&' when you call the function, unlike standard function calls which create a reference as long as it's mentioned in the function declaration.
Link to comment
Share on other sites

Here's how:

[code]foo(array(
  'result' => &$result,
));

function foo($args) {
  $result = &$args['result'];
}[/code]

Because the reference is in the array it remains a reference, even though the array itself was passed copy-on-write.  The referencing must be done in both places, as the variable would normally be copied both during the function call and during the assignment within the function.
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.