SchweppesAle Posted January 2, 2010 Share Posted January 2, 2010 Was just wondering when it's appropriate to use references in PHP. I've read a few articles that say PHP uses some sort of "clever" algorithm which makes it faster to simply copy the variable then pass it by reference. Quote Link to comment https://forums.phpfreaks.com/topic/186943-php-and-references/ Share on other sites More sharing options...
Mchl Posted January 2, 2010 Share Posted January 2, 2010 PHP automatically passes all objects through reference, and that's about 90% of cases you need it. Other 10% is for whenever you want a function to return a result of its actions through argument (like preg_match_all or shuffle). As to whether one is faster than other or not... that's hardly relevant, because you should choose method basing on what you want to achieve and not on performance. Quote Link to comment https://forums.phpfreaks.com/topic/186943-php-and-references/#findComment-987184 Share on other sites More sharing options...
SchweppesAle Posted January 2, 2010 Author Share Posted January 2, 2010 PHP automatically passes all objects through reference, and that's about 90% of cases you need it. Other 10% is for whenever you want a function to return a result of its actions through argument (like preg_match_all or shuffle). As to whether one is faster than other or not... that's hardly relevant, because you should choose method basing on what you want to achieve and not on performance. If PHP automatically passes them through reference then why use the & character? Also, if I wanted to delete a series of database entries, I could concatinate one huge query string which does it all in one shot or I can run 200 queries for each database entry. How is performance not an issue? Quote Link to comment https://forums.phpfreaks.com/topic/186943-php-and-references/#findComment-987210 Share on other sites More sharing options...
mikesta707 Posted January 2, 2010 Share Posted January 2, 2010 PHP automatically passes objects by reference, not all variables. You use the & character for variables that are not objects, and the & does a little more than just passing by reference. (check out the manual for more information) As far as speed, yes speed is important when you are programming, but the pass by value vs. by reference decision is made when you need the functionality of one over the other. When you need too pass by reference, you do, and likewise, if you need to pass by value, you do. Also the example you cited had nothing to do with passing by reference or value, so I don't see how its relevant Quote Link to comment https://forums.phpfreaks.com/topic/186943-php-and-references/#findComment-987211 Share on other sites More sharing options...
Mchl Posted January 2, 2010 Share Posted January 2, 2010 Also, if I wanted to delete a series of database entries, I could concatinate one huge query string which does it all in one shot or I can run 200 queries for each database entry. How is performance not an issue? That's hardly the same situation. You can create such a query in a variety of ways, using variable references or not. Quote Link to comment https://forums.phpfreaks.com/topic/186943-php-and-references/#findComment-987218 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.