drunkelf Posted November 12, 2012 Share Posted November 12, 2012 I quaote When you pass an argument to the function, a local copy is made in the function to store the value. Any changes made to that value affect only the local copy of the variable in the function, not the source of the parameter. You can define parameters that modify the source variable by defining reference parameters. Reference parameters define references by placing an ampersand (&) directly before the parameter in the function’s definition. some code <?php function capitalize( &$str, $each=TRUE ) { // First, convert all characters to lowercase $str = strtolower($str); if ($each === true) { $str = ucwords($str); } else { $str{0} = strtoupper($str{0}); } } $str = "hEllo WoRld!"; capitalize( $str ); echo $str; ?> Ok I do not undestand reference parameters.CAn sombody help me out.I am going to continue with other lessons but would like to have a better understanding of this reference parameters.Thanks Quote Link to comment https://forums.phpfreaks.com/topic/270591-help-me-understand/ Share on other sites More sharing options...
AyKay47 Posted November 12, 2012 Share Posted November 12, 2012 I will try to simplify this for you. These lines are of significance: $str = "hEllo WoRld!"; capitalize( $str ); echo $str; The source of the parameter sent to the function "capitalize" is $str = "hEllo WoRld!"; If the source $str was not passed to the function by reference, when you output $str it would remain "hEllo WoRld" since only a copy local to the function will be changed and not the source. However since you are passing the source to the function by reference, the source $str changes as well. In this particular case when you output $str you will get "HELLO WORLD". Quote Link to comment https://forums.phpfreaks.com/topic/270591-help-me-understand/#findComment-1391813 Share on other sites More sharing options...
JonnoTheDev Posted November 12, 2012 Share Posted November 12, 2012 The word to describe this behavior is, 'scope'. Variables that are used inside a function are not in the same scope as variables defined in the global scope i.e the page calling the function. Therefore a variable set in the global scope cannot be affected by setting the value of a variable of the same name inside a function UNLESS it is passed into the function by reference, 'using & as a prefix to the variable name does this'. Read this http://php.net/manual/en/language.variables.scope.php Quote Link to comment https://forums.phpfreaks.com/topic/270591-help-me-understand/#findComment-1391838 Share on other sites More sharing options...
JonnoTheDev Posted November 12, 2012 Share Posted November 12, 2012 One thing to note on this subject is that passing variables around by reference is really a depreciated / clumsy way of doing things. So is using global variables inside of functions. A functions job is to contain its workings so that it it can be used over and over again without having any affect on code outside of its scope. A function should return information and work with any information that is passed into it. So, your capitalize function should be written as follows: <?php function capitalize($str) { $str = strtolower($str); return ucwords($str); } ?> And the calling code is <?php $string = "HEllo WOrld"; $new_string = capitalize($string); echo $string . '<br />' . $new_string; ?> Quote Link to comment https://forums.phpfreaks.com/topic/270591-help-me-understand/#findComment-1391842 Share on other sites More sharing options...
AyKay47 Posted November 12, 2012 Share Posted November 12, 2012 (edited) drunkelf, If you would like to study further what neil is talking about, the buzz words are encapsulation and loose coupling Edited November 12, 2012 by AyKay47 Quote Link to comment https://forums.phpfreaks.com/topic/270591-help-me-understand/#findComment-1391844 Share on other sites More sharing options...
JonnoTheDev Posted November 12, 2012 Share Posted November 12, 2012 http://schlueters.de/blog/archives/125-Do-not-use-PHP-references.html Quote Link to comment https://forums.phpfreaks.com/topic/270591-help-me-understand/#findComment-1391845 Share on other sites More sharing options...
Andy-H Posted November 12, 2012 Share Posted November 12, 2012 (edited) echo '<pre>'; $hello = 'hello world!'; echo 'First we\'re going to pass by value: '. PHP_EOL; echo "\t" . 'We are in the global scope: $hello = '. $hello . PHP_EOL; function ucase($hello) { $hello = strtoupper($hello); echo "\t\t" . 'We are in the local scope of the function "ucase": $hello = '. $hello . PHP_EOL; } echo "\t" . 'We are back in the global scope: $hello = '. $hello . PHP_EOL . PHP_EOL; echo 'Now we\'re going to pass by reference: '. PHP_EOL; echo "\t" . 'We are in the global scope: $hello = '. $hello . PHP_EOL; function ucase(&$hello) { $hello = strtoupper($hello); echo "\t\t" . 'We are in the local scope of the function "ucase" passing by reference: $hello = '. $hello . PHP_EOL; } echo "\t" . 'We are back in the global scope after passing to "ucase" by reference: $hello = '. $hello . PHP_EOL . PHP_EOL; Try running this, hopefully it will help Edited November 12, 2012 by Andy-H Quote Link to comment https://forums.phpfreaks.com/topic/270591-help-me-understand/#findComment-1391854 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.