I want to increase $x by 1:
$x = 1;
function addOne($x) {
$x++;
return $x;
}
$x = addOne($x);
Is there a way to do this with references, so I don't need to return $x, I can just write addOne($x)?
This is what you're looking for, a by-ref parameter indicated by &.
$x = 1;
function addOne(&$x) {
$x++;
}
addOne($x);
Some notes:
addOne(5) would throw a fatal exceptionfunction &foo()).More info on references: http://php.net/manual/en/language.references.php
function addOne($x) but then just call it with addOne(&$x)?$x = 1;
function addOne(&$x) {
$x++;
}
addOne($x);
The & sign shows that it takes the parameter by reference. So it increments $x in the function and it will also affect the $x variable in the calling scope.
See also: http://php.net/references for a quick overview about them.
$x++;:D