Returning an array from PHP function

KevDog
KevDog's picture

Joined: 2003-06-03
Posts: 26
Posted: Thu, 2003-11-13 15:13

I was doing some tweaking of gallery, however not being too adept in php, Im having problems returning an array of values from a custom function. I know how to do this in C, however php doesnt seem to have an equivalent *pointer. What's the best way to do this?

Thanks

 
beckett
beckett's picture

Joined: 2002-08-16
Posts: 3474
Posted: Thu, 2003-11-13 16:04

PHP is very loosely typed. Arrays, strings, ints, typing is mostly transparent. If your variable is an array, then $variable is in fact a pointer (okay, it's really a symbol reference) to your array construct, and it is dereferencable. Just like in C, where array[x] is the same as (type) *(&array+x*sizeof(type)), in PHP $array[$x] gives you your value, though unlike C, you can't actually look at the pointer and see what memory address it maps to (because it's not a pointer, but a "name").

function foo($string) {
   return explode(' ', $string);
}
$blah = 'They must bust in early May.';
$myGenericVariable = foo($blah);
print_r($myGenericVariable);

will print out

Array
(
    [0] => They
    [1] => must
    [2] => bust
    [3] => in
    [4] => early
    [5] => May.
)

Does that answer your question? Probably not... so post back w/more details. :)

-Beckett (

)

 
KevDog
KevDog's picture

Joined: 2003-06-03
Posts: 26
Posted: Thu, 2003-11-13 19:37

I better be more explicit.

Im sorry I dont have the code in front of me since Im at work and the exact code is at home, however Ill do my best to improvise.

I basically have something like this:


include('defaults.inc')

class sidebar{

  var dir_list=array();

   function dir_listing(){
       ...
       ...
       preg_match_all($pattern, $subject, $matches);
       ....Do some various filtering of the matches....
 
     return($matches);
       
   }

   function sidebar(){

       $this->dir_list=dir_listing();
   }
 

}

In the above code if the count of $matches is 6 (for example), the only thing I get back is $matches[5] -- and hence the count of dir_list always equals 1. I want the the whole $matches array return, not just the last value.

PS One last thing - kind of unrelated. I included at the top the file defaults.inc. As you can imagine this basically is a php file with stated defaults, such as $home='http://abc.com'; $main_dir='c:\\temp'; etc.

The problem is that these variables are not seen inside the scope of the class. I tried moving the include statement into the constructor function of the class, however once again, these variables were only available inside the scope of the constructor, not in the other member functions. What is the best way to make the default variables available to all class member functions (using an include statement), without having to explicitly set each variable in a constructor function for example? Again I thought it would be great to place all the defaults located in the default.inc file, inside a default array. Inside the class, I could have a class member variable declared as $def, and then call a specific function located inside default.inc that would set $def=$default_array. Again however this would require returning an entire array from a function.

 
beckett
beckett's picture

Joined: 2002-08-16
Posts: 3474
Posted: Fri, 2003-11-14 08:20

Well, if you have six matches, then the returned array should contain six elements, 0-->5. Can you debug the exact returned array?

$debug = dir_listing();
print '<pre>';
print_r($debug);
print '</pre>';
exit;

You're correct about the included variables being out of scope, since they're not declared as globals. Remember... to preserve the sense of a class entity, all class variables should be declared inside the class itself. So you should declare them inside the class, outside of the constructor. Then the constructor should initialize them individually. You could bunch the class variables together into an array, and pass the array out to another function, if you like, but that sounds more complicated. (Though you could pass the array by reference (&$array), so you're not copying huge amounts of data around the place). Global variables you should consider making truly global. Make sure you're declaring all of your variables where it makes most sense.

Since this isn't really a PHP help forum, you'll probably have more success over on one of the PHP help sites, or on IRC (#php on freenode.net). We try to stick to Gallery-related stuff over here. :)

-Beckett (

)