note 123941 added to function.array-push

From: Date: Thu, 13 Jun 2019 21:38:21 +0000
Subject: note 123941 added to function.array-push
Groups: php.notes 
Request: Send a blank email to php-notes+get-211254@lists.php.net to get a copy of this message
There is problem with pushing references to array, introduced in PHP 5.4 - did someone decide it is not needed? In PHP 5.3 this could be used: $A=array(); array_push($A,1); $c=2; array_push($A,&$c); print_r($A); $c=3; print_r($A); Outputs correctly: Array ( [0] => 1 [1] => 2 ) Array ( [0] => 1 [1] => 3 ) Think of Reference as a pointer in other languages... This function is needed for example to push parameters for MySql query: $params=array(); array_push($params,&$field1); array_push($params,&$field2); array_unshift($params,'ss'); call_user_func_array(array($Query,'bind_param'),$params); This code causes fatal error in PHP 5.4 and depending on server configuration it may not even be reported why... A workarround to allow pushing references to array is this: $A=array(); $A[]=1; $c=2; $A[]=&$c; print_r($A); $c=3; print_r($A); $params=array(); $params[]=&$field1; $params[]=&$field2; array_unshift($params,'ss'); call_user_func_array(array($Query,'bind_param'),$params); (in actual code, the fields are specified dynamically and iterated in for-loop...) This seems working both on PHP 5.3 and PHP 5.6 ... ---- Server IP: 208.43.231.9 Probable Submitter: 185.85.3.195 (proxied: 79.98.159.114) ---- Manual Page -- http://php.net/manual/en/function.array-push.php Edit -- https://master.php.net/note/edit/123941 Del: integrated -- https://master.php.net/note/delete/123941/integrated Del: useless -- https://master.php.net/note/delete/123941/useless Del: bad code -- https://master.php.net/note/delete/123941/bad+code Del: spam -- https://master.php.net/note/delete/123941/spam Del: non-english -- https://master.php.net/note/delete/123941/non-english Del: in docs -- https://master.php.net/note/delete/123941/in+docs Del: other reasons-- https://master.php.net/note/delete/123941 Reject -- https://master.php.net/note/reject/123941 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#211254) next »