HuggieBear Posted November 8, 2006 Share Posted November 8, 2006 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?RegardsHuggie Quote Link to comment Share on other sites More sharing options...
trq Posted November 8, 2006 Share Posted November 8, 2006 [code=php:0]function drawForm($location="") {[/code]You really ought to read the [url=http://php.net/functions]manual[/url] aswell, functions are really one of the basics. Quote Link to comment Share on other sites More sharing options...
HuggieBear Posted November 8, 2006 Author Share Posted November 8, 2006 Thanks Thorpe,I did read the manual, and I saw the bit about default variables, but I guess I didn't take it in. Think I'll head to bed and pick this up in the morning.RegardsHuggie Quote Link to comment Share on other sites More sharing options...
allaboutthekick Posted November 8, 2006 Share Posted November 8, 2006 yo huggie i cant send you a PM because of security issues please send me an email to regrethedays@tmail.com and we will talk thanks. Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted November 8, 2006 Share Posted November 8, 2006 I like to set my default parameters to NULL instead of '' or 0. Not sure if it makes any big difference though. Quote Link to comment Share on other sites More sharing options...
btherl Posted November 9, 2006 Share Posted November 9, 2006 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.. Quote Link to comment Share on other sites More sharing options...
trq Posted November 9, 2006 Share Posted November 9, 2006 But then you'd need to call a function (even with only one arguments) like....[code=php:0]$result = foo(array("arg"));[/code]wouldn't you? Quote Link to comment Share on other sites More sharing options...
btherl Posted November 9, 2006 Share Posted November 9, 2006 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] Quote Link to comment Share on other sites More sharing options...
doni49 Posted November 9, 2006 Share Posted November 9, 2006 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 Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted November 9, 2006 Share Posted November 9, 2006 [code]<?phpfunction 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? Quote Link to comment Share on other sites More sharing options...
btherl Posted November 9, 2006 Share Posted November 9, 2006 [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. Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted November 9, 2006 Share Posted November 9, 2006 I still don't see how it's pass by reference. By the time you make the assignment you are working on what's already a local value to the function. Perhaps I'm missing something here. Quote Link to comment Share on other sites More sharing options...
btherl Posted November 9, 2006 Share Posted November 9, 2006 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. Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted November 9, 2006 Share Posted November 9, 2006 K, that's the missing piece I was looking for. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.