note 126633 added to language.references.pass

From: Date: Tue, 23 Nov 2021 13:22:11 +0000
Subject: note 126633 added to language.references.pass
Groups: php.notes 
Request: Send a blank email to php-notes+get-217285@lists.php.net to get a copy of this message
A reference remains a reference, even if it's an element of an array that is not passed by reference. An example: <?php $c = 3; $array = array( 'a' => 1, 'b' => 2, 'c' => &$c ); var_dump($array); function func($array) { $array['a'] = 4; $array['b'] = 5; $array['c'] = 6; var_dump($array); } func($array); var_dump($array); var_dump($c); ?> This will output the following: array(3) { ["a"]=> int(1) ["b"]=> int(2) ["c"]=> &int(3) } array(3) { ["a"]=> int(4) ["b"]=> int(5) ["c"]=> &int(6) } array(3) { ["a"]=> int(1) ["b"]=> int(2) ["c"]=> &int(6) } int(6) You could use this to allow a function to have read-write access to part of the array, while limiting it to read-only access for the rest of the array: <?php $config = [ 'ro' => [ // ... ], 'rw' => [ // ... ] ]; $copy = $config; $copy['rw'] = &$config['rw']; func($copy); ?> ---- Server IP: 45.112.84.4 Probable Submitter: 45.112.85.242 (proxied: 2a01:cb01:3026:1b2d:8219:34ff:fe91:4a6a) ---- Manual Page -- https://php.net/manual/en/language.references.pass.php Edit -- https://main.php.net/note/edit/126633 Del: integrated -- https://main.php.net/note/delete/126633/integrated Del: useless -- https://main.php.net/note/delete/126633/useless Del: bad code -- https://main.php.net/note/delete/126633/bad+code Del: spam -- https://main.php.net/note/delete/126633/spam Del: non-english -- https://main.php.net/note/delete/126633/non-english Del: in docs -- https://main.php.net/note/delete/126633/in+docs Del: other reasons-- https://main.php.net/note/delete/126633 Reject -- https://main.php.net/note/reject/126633 Search -- https://main.php.net/manage/user-notes.php

« previous php.notes (#217285) next »