3 ms·
Not sure I understand you. A concrete PHP example: function noop($a, $b) { $tmp = $a; $a = $b; $b = $tmp; } function swap(&$c, &$d) {
by paulddraper 10y ago
Not sure I understand you. A concrete PHP example:
function noop($a, $b) {
$tmp = $a;
$a = $b;
$b = $tmp;
}
function swap(&$c, &$d) {
$tmp = $c;
$c = $d;
$d = $tmp;
}
$first = [1, 2];
$second = [3, 4];
noop($first, $second);
swap($first, $second);
$a and $b work much differently than $c and $d.
I would call the first two pointers (to [1,2] and [3,4]), and the latter two references (to $first and $second). What would you call them?
(Note: the latter function is impossible to write in Java.)
- coldtea 10y ago>I would call the first two pointers (to [1,2] and [3,4]), and the latter two references (to $first and $second). What would you call them? In the first case, $a and $b pass the arrays by copying their values. The second example uses references. Java can't do the change visible externally because it passes its references by value -- but that's not a necessity for all languages with references. As the PHP manual itself says: "References in PHP are a means to access the same variable content by different names. They are not like C pointers; for instance, you cannot perform pointer arithmetic using them, they are not actual memory addresses, and so on".