note 68835 added to language.references.pass

From: Date: Sat, 12 Aug 2006 16:32:43 +0000
Subject: note 68835 added to language.references.pass
Groups: php.notes 
Request: Send a blank email to php-notes+get-115830@lists.php.net to get a copy of this message
Some have noticed that reference parameters can not be assigned a default value. It's actually wrong, they can be assigned a value as the other variables, but can't have a "default reference value", for instance this code won't compile : <?php function use_reference( $someParam, &$param =& $POST ) { ... } ?> But this one will work : <?php function use_reference( $someParam, &$param = null ) ?> So here is a workaround to have a default value for reference parameters : <?php $array1 = array ( 'test', 'test2' ); function AddTo( $key, $val, &$array = null) { if ( $array == null ) { $array =& $_POST; } $array[ $key ] = $val ; } AddTo( "indirect test", "test", $array1 ); AddTo( "indirect POST test", "test" ); echo "Array 1 " ; print_r ( $array1); echo "_POST "; print_r( $_POST ); ?> And this scripts output is : Array 1 Array ( [0] => test [1] => test2 [indirect test] => test ) _POST Array ( [indirect POST test] => test ) Of course that means you can only assign default reference to globals or super globals variables. Have fun ---- Server IP: 66.163.161.117 Probable Submitter: 82.231.150.241 ---- X-Spam-Status: No, hits=3.1 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER autolearn=no version=2.64 ---- Manual Page -- http://www.php.net/manual/en/language.references.pass.php Edit -- https://master.php.net/note/edit/68835 Del: integrated -- https://master.php.net/note/delete/68835/integrated Del: useless -- https://master.php.net/note/delete/68835/useless Del: bad code -- https://master.php.net/note/delete/68835/bad+code Del: spam -- https://master.php.net/note/delete/68835/spam Del: non-english -- https://master.php.net/note/delete/68835/non-english Del: in docs -- https://master.php.net/note/delete/68835/in+docs Del: other reasons-- https://master.php.net/note/delete/68835 Reject -- https://master.php.net/note/reject/68835 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#115830) next »