Jump to content

Help Me Understand


drunkelf

Recommended Posts

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

Link to comment
Share on other sites

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".

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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;
?>

Link to comment
Share on other sites

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 by Andy-H
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.