Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

global variable question

Status
Not open for further replies.

kathyc20

Programmer
Mar 7, 2001
96
CA
I have a function called ReadOldPriceList() that reads in a flat text file
of stock prices and puts them into an array called $iOldPrices.

Inside my function, I have declared my array as follows


function ReadOldPriceList()
{
global $iOldPrices;
$iOldPrices = array();

.....

}

and assign it values after I parse the flat text file.

I've tested my array inside my function and I'm getting
the values put properly into the array.

But what I need to do is access this array later on
in my script and when I try to print out the values later
on to test it, it's empty.


for ($iCount = 0; $iCount < count($iOldPrices); $iCount++)
{
print &quot;$iOldPrices[$iCount]&quot;;
}

Can someone please let me know what I've done wrong.

 
I can't reproduce your error. I'm running PHP 4.2.1, what version are you on? ______________________________________________________________________
Perfection in engineering does not happen when there is nothing more to add.
Rather it happens when there is nothing more to take away.
 
I'm not that familiar with the quirks of PHP 3. I'm still coming to grips with the quirks of PHP 4.

Try initializing the array outside your function, then using it as global inside your function. ______________________________________________________________________
Did you know?
The quality of our answers is directly proportional to the number of stars you vote?
 
Even if you use the &quot;global&quot; statement, try to send your variable value thanks to a &quot;return&quot; :

function ReadOldPriceList()
{
$iOldPrices = array();
.....
return $iOldPrices;
}

$iOldPrices = ReadOldPriceList();

Ced.
 
ccherchi is right; to make your functions as modular and independent as possible, this is the right way to go. (in fact, don't even use the &quot;global&quot; statement) Otherwise, you run the risk of introducing non-obvious bugs, when you change a function, or change a variable in your main code block.

I understand that there are occasions where you want to directly modify a global variable inside a function, but this should be used with extreme care. For the most part, global variables should be considered as messy, cluttering up your application.

Also, you should consider moving your application to PHP4 soon!!! PHP3 has a few security holes, and is VERY underpowered, compared to PHP4. Plus, the functionality for PHP will be progressing even further soon from Zend2/PHP4.3 and up. Every delay will only make the final upgrade more painful. -------------------------------------------

&quot;Now, this might cause some discomfort...&quot;
(
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top