Jump to content

How to return an entire array from a PHP Function?


SaranacLake

Recommended Posts

I hate array...   😞

So I had a block of code inside my photo-gallery.php script that took the path to my photos directory, and went to that directory, and then read all of the photo filenames into an array.  Then in my HTML, I iterate through this array to display all of the photos for my gallery.

Now I would like to move that code to an included file so multiple scripts can access it and always be working with the same array.

It seems to me that I need to encapsulate my code inside a function?

Then I could call my getPhotoFilesArray back to my callings cript, and use that array for whatever.

I haven't coded PHP in like 4 years and I am struggling to return the entire array back to my caling script.

This is what I have so far...

	function getPhotoFilesArray($photoPath){
	     $photoFiles = array();
	     <code to find corresponding files>
	     $photoFiles gets populated in a loop
	     return $photoFiles;
	}
	

 

Then in my calling script, I have...

	<?php
	     require_once('../../../secure_outside_webroot/config.php');
	     require_once(WEB_ROOT . 'utilities/functions.php');
	     getPhotoFilesArray($photoPath);
	     var_dump($photoFiles);
	

 

I get some error...

	Notice: Undefined variable: phtoFiles in photo-gallery.php line 133 (which is my var_dump).
	

 

<br>

Would appreciate help getting this to work!

 

 

Edited by SaranacLake
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.