perryc Posted June 19, 2010 Report Posted June 19, 2010 Can someone help me with passing functions by reference in php. having a hard time trying to get a grasp around the idea of reference's and what i would use them for in everyday php coding. Thanks, Cody Perry Quote
falkencreative Posted June 19, 2010 Report Posted June 19, 2010 Pass by value looks like this: <?php function test($temp) { $temp++; } $i = 0; test($i); echo $i; // displays "0" ?> There's no connection between the $temp variable within the function and the $i variable outside, so echoing out $i results in "0". $temp only exists within the test() function and disappears once the function ends. Here's pass by reference: <?php function test(&$test) { $test++; } $i = 0; test($i); echo $i; // displays "1" ?> Note the "&" before the variable name in test(&$test). That means that when you pass in the variable, rather than creating a brand new variable, $temp references the $i variable outside the function. Any changes made to $temp within the function will also affect $i. How is this helpful? I'm not sure, it depends on what you need coded. I've really never used it in my PHP code (yet, at least) but I have used it quite often in C++. It has come in handy for me on several occasion. For example, consider this situation... You have a function that should return a valid integer (0 through whatever). However, there is also a chance that the number you are looking for doesn't exist. How do you code your function? If the variable doesn't exist and the function fails, you can't return 0 (false), since that is a valid integer and could be confused. However, you could do something like this: <?php function get_variable($temp) { if (variable exists) { $temp = variable; return true; } else { return false; } } $i = ''; if (get_variable($i)) { // get_variable() returns true // $i will contain the variable you were looking for } else { // get_variable() returns false // $i still contains '' } ?> Does that make a bit more sense? Quote
perryc Posted June 19, 2010 Author Report Posted June 19, 2010 A little bit the way the book explains the example is like a matchbox with a string attached to it I got real confused at first. Then i went back and read the information step by step. So i understand it a little bit better now. Im learning Including and Requiring files section on my chapter right now so hopefully i don't have any more trouble with this chapter i fell real stupid sometimes asking questions like these to you guys. Thanks, Cody Perry Quote
falkencreative Posted June 19, 2010 Report Posted June 19, 2010 fell real stupid sometimes asking questions like these to you guys. Better that you ask questions and feel a little silly but get a better understanding of the concepts than to not ask questions and not understand at all. Feel free to ask questions - that's what we are here for. Quote
perryc Posted June 19, 2010 Author Report Posted June 19, 2010 Lol, thanks. Ya im hoping i can finishes this book by my birthday which is this friday. Then i have a more advanced php book then lol. Thanks. Cody Perry Quote
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.
Note: Your post will require moderator approval before it will be visible.