colbyg Posted March 24, 2009 Share Posted March 24, 2009 Like... <?php function &myfunction(){ echo 'idk'; } ?> Link to comment https://forums.phpfreaks.com/topic/150820-what-does-the-ampersand-sign-in-front-of-a-function-name-do/ Share on other sites More sharing options...
genericnumber1 Posted March 24, 2009 Share Posted March 24, 2009 Return a reference. <?php function &testFunction(&$testReference) { return $testReference; } $test = 'hello'; $reference =& testFunction($test); $reference = 'world'; echo $test . ' ' . $reference; The above code does not output "hello world", it outputs "world world" because testFunction returned a reference to $test. It doesn't appear particularly useful in this circumstance, but it can be quite useful in some situations. Link to comment https://forums.phpfreaks.com/topic/150820-what-does-the-ampersand-sign-in-front-of-a-function-name-do/#findComment-792337 Share on other sites More sharing options...
corbin Posted March 24, 2009 Share Posted March 24, 2009 Wow never knew PHP could do that >.<. Link to comment https://forums.phpfreaks.com/topic/150820-what-does-the-ampersand-sign-in-front-of-a-function-name-do/#findComment-792353 Share on other sites More sharing options...
genericnumber1 Posted March 24, 2009 Share Posted March 24, 2009 Wow never knew PHP could do that >.<. I'd imagine it was more useful in php4 when all classes weren't passed by reference automatically. Although I suppose it could be useful to retrieve references to certain array elements so that they can be modified. Something like... <?php function &findValue(&$array, $search, &$found = false) { foreach($array as &$valRef) { if($valRef == $search) { $found = true; $result =& $valRef; break; } elseif(is_array($valRef)) { $result =& findValue($valRef, $search, $found); if($found) break; } } if($found) { return $result; } else { return $found; } } $array = array( 'Hello', 'World', array( 'DeeperHello', 'DeeperWorld' ) ); $result =& findValue($array, 'DeeperHello', $found); if($found) { $result = 'ModifiedDeeperHello'; } print_r($array); /* Array ( [0] => Hello [1] => World [2] => Array ( [0] => ModifiedDeeperHello [1] => DeeperWorld ) ) */ Of course it's messy, but you get the idea. I haven't used it before, but you never know. Link to comment https://forums.phpfreaks.com/topic/150820-what-does-the-ampersand-sign-in-front-of-a-function-name-do/#findComment-792364 Share on other sites More sharing options...
corbin Posted March 24, 2009 Share Posted March 24, 2009 Hrmmmm another strange feature of PHP lol. Link to comment https://forums.phpfreaks.com/topic/150820-what-does-the-ampersand-sign-in-front-of-a-function-name-do/#findComment-792381 Share on other sites More sharing options...
colbyg Posted March 24, 2009 Author Share Posted March 24, 2009 so whatever the function returns can be directly modified by changing the value of the variable you assign it to? Link to comment https://forums.phpfreaks.com/topic/150820-what-does-the-ampersand-sign-in-front-of-a-function-name-do/#findComment-792527 Share on other sites More sharing options...
colbyg Posted March 24, 2009 Author Share Posted March 24, 2009 ahh i got it <?php $array = array('Colby','Evan'); function &test() { global $array; return $array[0]; } $test =& test(); $test = 'Tammy'; echo $array[0]; // prints 'Tammy' ?> thank yall! now i'll be late for school because i was doing this instead of getting ready Link to comment https://forums.phpfreaks.com/topic/150820-what-does-the-ampersand-sign-in-front-of-a-function-name-do/#findComment-792530 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.