5 Replies - 55905 Views - Last Post: 24 February 2011 - 11:28 PM Rate Topic: -----

#1 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10596
  • View blog
  • Posts: 39,259
  • Joined: 27-December 08

Creating Functions with Array as Parameter

Post icon  Posted 18 March 2009 - 09:01 AM

I'm not sure how to create a function that accepts an array as a parameter, but here's my best guess. Could someone please guide me on this?

function avg(array() $x){}


Is This A Good Question/Topic? 1
  • +

Replies To: Creating Functions with Array as Parameter

#2 William_Wilson  Icon User is offline

  • lost in compilation
  • member icon

Reputation: 205
  • View blog
  • Posts: 4,807
  • Joined: 23-December 05

Re: Creating Functions with Array as Parameter

Posted 18 March 2009 - 09:05 AM

php does not discriminate its variables like that, you can simply use function avg($x){} and you can pass an array.

You may also want to take a look at the is_array function:
http://ca2.php.net/is_array
Was This Post Helpful? 2
  • +
  • -

#3 roudard  Icon User is offline

  • D.I.C Head

Reputation: 17
  • View blog
  • Posts: 143
  • Joined: 26-September 05

Re: Creating Functions with Array as Parameter

Posted 18 March 2009 - 09:06 AM

hey,

in php, you don't need to declare the type of the parameters
so you can just write :
function avg($x){}



if you want ot make sure you have an array you can check the parameter with is_array() (php.net) at the beginning of the function

hope this helps
Was This Post Helpful? 1
  • +
  • -

#4 Mangotastic  Icon User is offline

  • New D.I.C Head

Reputation: 6
  • View blog
  • Posts: 46
  • Joined: 15-October 08

Re: Creating Functions with Array as Parameter

Posted 18 March 2009 - 09:26 AM

As was said above PHP is a typeless language. PHP doesn't care if you've got an array, resource, int or custom data type - you pass them all the same. The only difference you may notice between PHP and other languages is that PHP passes arrays "by value", that is, the entire array is copied over in to a local scope.

If you wish to pass an array by reference so that your function is working on the actual array your passing rather than making a seperate copy of it you do it like so (by prepending the argument with an amperstand);

function avg(&$x) {}

Was This Post Helpful? 2
  • +
  • -

#5 Guest_James*


Reputation:

Re: Creating Functions with Array as Parameter

Posted 24 February 2011 - 11:00 PM

Try this

function function_name($params) {
    echo $params->param1;
    echo $params->param2;
}
$params = (object) array('param1' => $value1, 'param2' => $value2);
call_user_func_array('function_name', array($params));


This post has been edited by CTphpnwb: 24 February 2011 - 11:27 PM
Reason for edit:: Fixed code tag

Was This Post Helpful? 1

#6 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3001
  • View blog
  • Posts: 10,393
  • Joined: 08-August 08

Re: Creating Functions with Array as Parameter

Posted 24 February 2011 - 11:28 PM

That may be helpful, but it's not generally a good idea to resurrect old topics.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1