Forum Moderators: coopster

Message Too Old, No Replies

Write values into an array.

         

opiston

2:15 am on Jan 27, 2006 (gmt 0)

10+ Year Member



Hi all
I have a question about writing values into array in php.

I have an array $test, the way $test was declared in file1.php:

$test = array();
and I set $test['value'] = "Josh".

I passed $test as a global to file2.php
However, when I do

$test['value'] = "Peter"

, it won't change the value of $test['value'].
$test['value'] still outputs Josh instead of Peter.

But if I do $db -> clear_cache()
then I can change the value of $test['value']
Could anyone tell me what the problem is?
I don't really want to do $db -> clear_cache()

Best Regards
Opiston

FalseDawn

2:30 am on Jan 27, 2006 (gmt 0)

10+ Year Member



I'm not sure where "$db -> clear_cache() " fits into it, but an array entry should be no different to a normal variable.

Try changing your declaration:
$test = array();
to
global $test = array();

And also, don't forget to put
global $test;

in the appropriate function in file2

opiston

5:37 am on Jan 27, 2006 (gmt 0)

10+ Year Member



Hi FalseDawn,

Thanks for helping.

Actually, I just realized that the value was written in. However, it changed back to the original value when the function is visited again.

For example:

function test() {
global $db, $test;
echo $test['value'];

//$db->clear_cache;

$test['value'] = "Peter";
echo $test['value'];
}

The first echo outputs Josh.
The second echo outputs Peter.
However, the next time I visit this function, the first echo outputs Josh instead of Peter.

But if I add that $db -> clear_cache; then the next time I vist the function test, the first echo will output Peter.

Best Regards
Opiston

opiston

6:02 am on Jan 31, 2006 (gmt 0)

10+ Year Member



Hi all,
The problem is solved.
Thanks for helping

Regards
Opiston