homepage Welcome to WebmasterWorld Guest from 54.227.20.250
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Sharing variables between functions
sharing variables betwee functions
Tourex

10+ Year Member



 
Msg#: 4002946 posted 2:01 pm on Oct 7, 2009 (gmt 0)

Can someone please enlighten this old idiot before I end up jumping off our office roof in frustration.

How I can share an array between multiple functions within a given script? Allow me to explain what I am trying to achieve...

The script processes order files uploaded by suppliers. Whenever the script finds a valid line, it calls a function that starts to build the final order, updating various fields in a main array ($IN['order-code'], $IN['quantity'], etc). Now, depending upon what it finds in the update file, it may call varous validation routines which in turn need to update the main $IN array, depending the options found. Finally, once the order has been built, another function uses the data in the $IN array to write the final order to the main tables.

In practise, there are a couple of different central arrays. I had hoped that declaring each as global at the top of each function would allow the data to be shared and updated by each function - but this doesn't always seem to be the case. Passing the arrays in the function parameters is fine when the data only needs to be read, but makes it cumbersom/unreliable when the function also needs to update any elements of the arrays.

Any help in explaining the best approach - in layman's language please - would be much appreciated.

Thanks in anticipation.

 

penders

WebmasterWorld Senior Member penders us a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



 
Msg#: 4002946 posted 11:43 pm on Oct 7, 2009 (gmt 0)

I had hoped that declaring each as global at the top of each function would allow the data to be shared and updated by each function - but this doesn't always seem to be the case.

If your array is global (ie. declared in the global scope and not locally to a function) then this should work. OR, use the $GLOBALS[] superglobal. eg. $GLOBALS['IN']['order-code']

NB: Every function that uses the global variable will need to declare it as global at the start of the function, otherwise a local variable will be created.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved