Jump to content

How do i pass array as an argument for a function ?


jd2007

Recommended Posts

<?php
class Arrays
{
private $arr=array();
private $mystr;

function produceStr(array $arr)
{
   $this->arr=$arr;
   $this->mystr=implode("and", $this->arr);
   echo $this->mystr;
}
}
?>

 

when i instantiate an object, i do this:

<?php
$a=new Arrays();
$b=array("a", "b", "c");
$a->produceStr($b);
?>

 

this doesn't work...why ?

Code works fine for me, using PHP5.2.x here. I noticed you are using the private keyword here for initiating variables. Make sure you are not using PHP4. PHP4 does not support the visibility keywords (private, protected and public), use var in replacement:

 var $arr=array();
var $mystr;

 

What results do you get?

 

EDIT: Didn't see the replies above

but i want to be array $arr but php treats it like string

Even if the you pass the produceStr function an array for the $arr argument $arr will still be an array, without 'array' before $arr.

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.