2

Assuming the following code:

<?php
function doStuff($rowCount) {
    $rowCount++;
    echo $rowCount.' and ';
    return $rowCount;
}
$rowCount = 1;
echo $rowCount.' and ';
doStuff($rowCount);
doStuff($rowCount);
doStuff($rowCount);
?>

The desired output is

1 and 2 and 3 and 4 and

The actual output is

1 and 2 and 2 and 2 and

I take it I'm musunderstanding how "return" works in this context. How could I best accomplish this?

1
  • 3
    i guess you need to review your understanding of variable scope, pass by value and reference concepts in php... Commented Aug 26, 2010 at 17:27

5 Answers 5

10

You either have to assign the return value of the doStuff calls back to the local $rowCount variable:

$rowCount = 1;
echo $rowCount.' and ';
$rowCount = doStuff($rowCount);
$rowCount = doStuff($rowCount);
$rowCount = doStuff($rowCount);

Or you pass the variable as a reference by putting a & in front of the formal parameter $rowCount:

function doStuff(&$rowCount) {
    $rowCount++;
    echo $rowCount.' and ';
    return $rowCount;
}

Now the formal parameter $rowCount inside the function doStuff refers to the same value as the variable that is passed to doStuff in the function call.

Sign up to request clarification or add additional context in comments.

Comments

2

You should try this :

$rowCount = 1;
echo $rowCount.' and ';
$rowCount = doStuff($rowCount);
$rowCount = doStuff($rowCount);
$rowCount = doStuff($rowCount);

Your doStuff() method returns an int that is never used when you simply use the statement doStuff($rowCount); without assignation.

Comments

1

change function doStuff($rowCount)

to function doStuff(&$rowCount)

Normally in PHP you're sending a copy of the variable to the function, so modifications within the function will not affect the value of the variable outside of the function. Adding the ampersand tells PHP to send a reference to the variable instead so modifications to the variable propagate back to the caller.

5 Comments

Why use this kind of method whereas doStuff already returns the new value of $rowCount ?
in your code, to what variable are you actually storing the returned value?
@ultrajohn - I'm not, the returned value gets dropped.
@Colin - I read too fast and didn't see the return value before I answered :)
oh, sorry, i thought @colin was the OP.. ;( @tloach, my question was actually address to Colin, whom i thought was the OP..
1

i would just try to fix the code: ...

<?php
function doStuff($rowCount) {
    $rowCount++;
    echo $rowCount.' and ';
    return $rowCount;
}
$rowCount = 1;
echo $rowCount.' and ';
doStuff(doStuff(doStuff($rowCount)));

?>

Comments

1

You need to pass the variable 'by reference' instead of 'by value' to accomplish this add an & to the variable in the function declaration:

function doStuff(&$rowCount);

Also check out PHP: Passing by Reference

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.