phppass-by-referenceparameter-passingpass-by-value

When should I pass by value and return and when pass by reference?


I ask this question because i learned that in programming and designing, you must have a good reason for decisions. I am php learner and i am at a crossroad here, i am using simple incrementation to try to get what im asking across. I am certainly not here to start a debate about the pros/cons of referencing, but when it comes to php, which is the better programming practice:

function increment(&$param) {
      ++$param;
}

vs.

function increment($param){
 return ++$param;
}

$param = increment($param);

Solution

  • First, references are not pointers.

    There are reasons why you should avoid references. Though they can simplify several algorithms, especially when you are manipulating two or more data structures that must have the same underlying data: