pzt_penguin Posted February 13, 2007 Share Posted February 13, 2007 Hi, I'm having a problem with using a object in a function. <? include 'test_db.php'; $query = ("Select * From $mytable WHERE username='$username'"); $data_result = mysql_query($query); $data = mysql_fetch_object($data_result); $data->gamesplayed=3; $data->gamesplayed++; echo "Games played before function = $data->gamesplayed<br>"; function test($name) { global $data,$opponent; echo "games played before = $name->gamesplayed<br>"; $name->gamesplayed=5; return $name->gamesplayed; } $bb=test($data); echo "your games played now = $bb<br>"; echo "Games played after function = $data->gamesplayed"; ?> output Games played before function = 4 games played before = 4 your games played now = 5 Games played after function = 4 My question is why dose data->gamesplayed go back to 4 after I run the function? I need it where data->gamesplayed will hold what alterations the function makes. Also when I use the function by just calling it like test($data) it still stays at 4. Thanks Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 13, 2007 Share Posted February 13, 2007 I think it's because you're passing it as a copy, not by reference. It gets confusing, but read this chapter: http://us3.php.net/manual/en/language.references.php and http://us3.php.net/manual/en/language.references.pass.php Quote Link to comment Share on other sites More sharing options...
pzt_penguin Posted February 13, 2007 Author Share Posted February 13, 2007 Thanks so much that worked. Quote Link to comment 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.