SaranacLake Posted December 6, 2019 Share Posted December 6, 2019 (edited) 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 December 6, 2019 by SaranacLake Quote Link to comment https://forums.phpfreaks.com/topic/309644-how-to-return-an-entire-array-from-a-php-function/ Share on other sites More sharing options...
chhorn Posted December 6, 2019 Share Posted December 6, 2019 2 hours ago, SaranacLake said: (which is my var_dump) It's not that var_dump you posted. Just have a full search over all files for "phtoFiles", looks like a spelling mistake. Quote Link to comment https://forums.phpfreaks.com/topic/309644-how-to-return-an-entire-array-from-a-php-function/#findComment-1572285 Share on other sites More sharing options...
Barand Posted December 6, 2019 Share Posted December 6, 2019 (edited) You need to assign the value returned by your function to a variable. $photoFiles = getPhotoFilesArray($photoPath); var_dump($photoFiles); I suggest you read up on how to use functions variable scope Edited December 6, 2019 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/309644-how-to-return-an-entire-array-from-a-php-function/#findComment-1572288 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.