Welcome to WebmasterWorld Guest from 126.96.36.199
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.
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.