Author Topic: Undefined number of arguments in PHP  (Read 677 times)

Offline Chris

  • Game Owner
  • Level 35
  • *
  • Posts: 2,217
  • Reputation: +28/-1
    • View Profile
Undefined number of arguments in PHP
« on: June 16, 2011, 05:29:20 AM »
In C there was an option for unlimited arguments to be passed to a function using "...". I don't remember how it was called and how it exactly worked. It was something like this:

Code: [Select]
void foo(...) { and a few lines with vSOMETHING() to strip the arguments were here }
Is there an equivalent of this in PHP? Any example/link?

Offline Nox

  • Level 35
  • **
  • Posts: 768
  • Reputation: +12/-2
    • View Profile
Re: Undefined number of arguments in PHP
« Reply #1 on: June 16, 2011, 05:32:53 AM »
Yes, in PHP you can supply any number of arguments above those required

To work with them then use http://cz2.php.net/manual/en/function.func-get-args.php along with other functions mentioned there

edit: no => yes to make it clearer :)
« Last Edit: June 16, 2011, 06:10:21 AM by Nox »
Meet us at an IRC irc.freenode.net #bbg as well
https://vimeo.com/36579366 (a must-watch) | Join BOINC - no longer a hype, but you can help never the less

Offline Barrikor

  • Level 21
  • *
  • Posts: 248
  • Reputation: +3/-0
    • View Profile
Re: Undefined number of arguments in PHP
« Reply #2 on: June 16, 2011, 12:09:54 PM »
I love those functions =)  Makes it so that if I want the first parameter to be optional I can =)
Projects: Pith Framework (at 0.5), CactusGUI (at 0.3)

Offline DV8

  • Level 10
  • *
  • Posts: 63
  • Reputation: +0/-0
    • View Profile
    • Shadowrun: Corrosion
Re: Undefined number of arguments in PHP
« Reply #3 on: June 17, 2011, 02:17:45 AM »
Am I the only one that thinks this is a bit, I don't know... messy? I think I'd rather solve it with an object or an array or something.

Offline pirategaspard

  • Level 9
  • *
  • Posts: 46
  • Reputation: +1/-0
    • View Profile
    • PointClickPress
Re: Undefined number of arguments in PHP
« Reply #4 on: June 17, 2011, 10:01:10 AM »
Am I the only one that thinks this is a bit, I don't know... messy? I think I'd rather solve it with an object or an array or something.

Agreed. In PHP when I need optional arguments on functions I pass in an array and then check for the keys in the array.
(This is where you could also set defaults if you'd like.) Makes things a bit more straightforward to work with.

Other benefits of passing an array is that you don't have to worry about the order in which you are passing the arguments and if you are using interfaces your interface won't change even if you pass more "arguments" through the array later on.


Offline Barrikor

  • Level 21
  • *
  • Posts: 248
  • Reputation: +3/-0
    • View Profile
Re: Undefined number of arguments in PHP
« Reply #5 on: June 18, 2011, 01:39:28 PM »
What about doing something like this:

Code: [Select]
<?php

function someFunction$first $second $third )
{
$numberOfDefinedArgs 3//number of param's in function declaration, (counting from 1 not 0)

$arrayOfUndefinedArgs array_slicefunc_get_args() , $numberOfDefinedArgs );
}

?>


(Note: I haven't tested it yet to make sure it works
Edited: to fix syntax )
« Last Edit: June 19, 2011, 07:33:31 PM by Barrikor »
Projects: Pith Framework (at 0.5), CactusGUI (at 0.3)

Offline Chris

  • Game Owner
  • Level 35
  • *
  • Posts: 2,217
  • Reputation: +28/-1
    • View Profile
Re: Undefined number of arguments in PHP
« Reply #6 on: June 19, 2011, 02:30:21 AM »
Yes, in PHP you can supply any number of arguments above those required
Really? How? It seems to me as if PHP could only pass a variable number of arguments but without making any obligatory arguments.

PHP
Code: [Select]
function sum() {
$count = func_num_args();
$sum = 0;
for($i=0; $i<$count; $i++) {
$num = func_get_arg($i);
$sum += $num;
}
return $sum;
}

C
Code: [Select]
void write_Log(int log_flag, const char* ptr, ...)
{
va_list ap;
va_start(ap, ptr);
... // log_flag is defined
}

 


SimplePortal 2.3.3 © 2008-2010, SimplePortal