phprocker Posted January 17, 2010 Share Posted January 17, 2010 Hey all. My question is: When creating a custom function in pHp, why would a person use an & symbol before the arguments? e.g. function newfunction(&$argument1, &$argument2) { } Thanks. Link to comment https://forums.phpfreaks.com/topic/188753-quick-and-easy-function-question/ Share on other sites More sharing options...
trq Posted January 17, 2010 Share Posted January 17, 2010 To pass the arguments by reference. See here. Link to comment https://forums.phpfreaks.com/topic/188753-quick-and-easy-function-question/#findComment-996397 Share on other sites More sharing options...
Andy-H Posted January 17, 2010 Share Posted January 17, 2010 It is to indicate that the variable is being passed by reference rather than by value. Basically the function is passed the memory address of the variable in the servers' RAM and adding another 'name' by which to access it in the file table. This way when the function updates the variable in the functions' scope and passes back to the local scope in which the function was called, the modifications to the referenced variable are still in effect. Heres an example you can run to get an idea of how it works. It also uses recursion. <?php function incrementToTen( & $i ) { if ( !is_int($i) ) return; if ( $i < 10 ) { echo 'i = ' . ++$i . '<br >'; incrementToTen($i); } } $n = 0; incrementToTen($n); echo '<br >n = ' . $n; ?> Link to comment https://forums.phpfreaks.com/topic/188753-quick-and-easy-function-question/#findComment-996401 Share on other sites More sharing options...
phprocker Posted January 17, 2010 Author Share Posted January 17, 2010 Thank you for the replies and I understand. 1 more question: What if it were an @ symbol before the argument? e.g. myfunction(@$argument1, @$argument2) { } Thanks again. Link to comment https://forums.phpfreaks.com/topic/188753-quick-and-easy-function-question/#findComment-996405 Share on other sites More sharing options...
Andy-H Posted January 17, 2010 Share Posted January 17, 2010 The @ symbol is used to suppress errors so they don't display, although it is normally before a call is made to a built in php function that you see the @ symbol. I have never seen it used in the context of your example. http://php.net/manual/en/language.operators.errorcontrol.php // EDIT: now I have In the comments on the above link. /* dsbeam at gmail dot com 02-Sep-2009 02:22 Though error suppression can be dangerous at times, it can be useful as well. I've found the following statements roughly equivalent: */ if( isset( $var ) && $var === $something ) if( @$var === $something ) /* EXCEPT when you're comparing against a boolean value (when $something is false). In that case, if it's not set the conditional will still be triggered. I've found this useful when I want to check a value that might not exist: */ if( @$_SERVER[ 'HTTP_REFERER' ] !== '/www/some/path/file' ) // or when we want to see if a checkbox / radio button have been submitted with a post action if( @$_POST[ 'checkbox' ] === 'yes' ) // Just letting you guys know my findings, Link to comment https://forums.phpfreaks.com/topic/188753-quick-and-easy-function-question/#findComment-996406 Share on other sites More sharing options...
phprocker Posted January 17, 2010 Author Share Posted January 17, 2010 Great thank you much. Link to comment https://forums.phpfreaks.com/topic/188753-quick-and-easy-function-question/#findComment-996408 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.