Welcome to WebmasterWorld Guest from 54.198.92.22

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

passing false variable to command line script

     
3:54 pm on Jul 31, 2015 (gmt 0)

Senior Member from ES 

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month

joined:July 24, 2002
posts: 1129
votes: 2


hi,

is it possible to bypass a variable in the argv array?

i have a script which accepts up to 4 params

php script.php param1 param2 param3 param4

is it possible to pass one of these as false? like you can do with a function call

myfunction($param1, false, $param3, param4);

many thanks!
10:03 pm on July 31, 2015 (gmt 0)

Full Member

Top Contributors Of The Month

joined:Apr 11, 2015
posts: 323
votes: 24


You can "bypass" a variable in the "argv" array (ie. using the func_get_args() function to implement variable-length argument lists).

However, if you have already declared your function with 4 "fixed" parameters then you are not really using the "argv" array (or, you don't need to use it).

It looks like you are referring to "default parameters". With default params you don't need to pass the argument in the function call. It will "default" if omitted. However, for obvious reasons, default params can only come at the end of the parameter list, otherwise it is ambiguous which argument belongs to which parameter! So, in your example, you cannot make $param2 default without making $param3 and $param4 default as well.

For example:


function myFunction($param1, $param2, $param3='foo', $param4=false)


In the above example, $param3 and $param4 are both optional in the function call, since they have defaults applied in the function signature. So, myFunction() can be called with either 2, 3 or 4 arguments. If called with just 2 args then $param3 will default to (string)'foo' and $param4 will default to (bool)false. If called with 3 args then only $param4 will default.

IMO, if a boolean param is set to default then it should always default to (bool)false. But that's by no means a standard; just best practise.
10:32 pm on Aug 3, 2015 (gmt 0)

Full Member

Top Contributors Of The Month

joined:Apr 11, 2015
posts: 323
votes: 24


Dang, just twigged... "command line script" (face palm)! argv makes more sense now! Although the same principle applies...

Whether you can "bypass" one of these arguments really depends on the nature of your script. And when you say "bypass" I assume you mean that you would want to call it like this:


php script.php param1 param3 param4


In other words, miss out param2 (ie. "argument" #2) - which should default to (bool)false in your script?

You can only do this if param3 and param4 are mandatory (to avoid ambiguity). So that if only 3 arguments are passed to the script then you know that it is parameter #2 that needs to default.

I would assume that in your script you are assigning the elements of the $argv array to meaningful variables in your script. For now I shall call these $chocolate, $butter, $milk and $eggs respectively for params 1..4. $butter is the 2nd parameter and is optional.


// Note that $argc is at least 1, since the name of the script is counted as the first element
// Only 3 arguments are passed (1 is missing)
if ($argc == 4) {
$chocolate = $argv[1];
$butter = false; // Assume that parameter 2 is missing so default it
$milk = $argv[2];
$eggs = $argv[3];
}
// All 4 arguments are passed
elseif ($argc == 5) {
$chocolate = $argv[1];
$butter = $argv[2];
$milk = $argv[3];
$eggs = $argv[4];
}
else {
// Error - Incorrect number of arguments passed
}
6:51 am on Aug 4, 2015 (gmt 0)

Senior Member from ES 

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month

joined:July 24, 2002
posts: 1129
votes: 2


hi whitespace, sorry for not replying to first, but yes, i meant your second explanation.

after trial and error i pass a placeholder value as the param, e.g.

myscript.php value1 default value2 value3

and check that $argv[2] != 'default'

it seems hacky to me, but there you go ;)

tks for help!
8:19 am on Aug 4, 2015 (gmt 0)

Full Member

Top Contributors Of The Month

joined:Apr 11, 2015
posts: 323
votes: 24


I think that any method you implement to default a "middle" parameter is going to seem a bit "hacky". And possibly not particularly intuitive for users of the script, without some explicit instructions.

Ideally, any default (optional) parameters should come at the end of the list (just like function parameters - which, in some ways, is enforced by the compiler). You then simply need to check for existence and default that param if it doesn't exist.
9:05 am on Aug 4, 2015 (gmt 0)

Senior Member from ES 

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month

joined:July 24, 2002
posts: 1129
votes: 2


in my dreams.... i write beautifully formatted and commented code - however... reality is somewhat different! lol

you're right though, it's a clearer way of doing it - have made a note for future reference. thanks a lot for help!