PHP: pass a variable to a function, do something to the variable, return it back

前端 未结 5 1919
孤街浪徒
孤街浪徒 2021-01-22 11:39

Assuming the following code:



        
5条回答
  •  终归单人心
    2021-01-22 12:16

    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.

提交回复
热议问题