note 92019 added to function.settype

From: Date: Sun, 05 Jul 2009 17:42:38 +0000
Subject: note 92019 added to function.settype
Groups: php.notes 
Request: Send a blank email to php-notes+get-157942@lists.php.net to get a copy of this message
in response to NWdev, the problem with your original code was that the argument to your custom function is being passed _by value_ so the original variable remains unchanged. Simply changing your code to this will solve the problem: <?php //$numArray is generated by another process $numArray = array('13','14','33'); var_dump($numArray); // conversion function function str_to_int(&$val){ // note the ampersand (&) character before the parameter above // -> this makes sure the argument is passed _by reference_ // -> instead of by value settype($val,'int'); } array_walk($numArray,'str_to_int'); var_dump($numArray); /* outputs: array(3) { [0]=> string(2) "13" [1]=> string(2) "14" [2]=> string(2) "33" } array(3) { [0]=> int(13) [1]=> int(14) [2]=> int(33) } */ ?> Regards, David ---- Server IP: 87.124.35.190 Probable Submitter: 81.141.250.159 ---- Manual Page -- http://www.php.net/manual/en/function.settype.php Edit -- https://master.php.net/note/edit/92019 Del: integrated -- https://master.php.net/note/delete/92019/integrated Del: useless -- https://master.php.net/note/delete/92019/useless Del: bad code -- https://master.php.net/note/delete/92019/bad+code Del: spam -- https://master.php.net/note/delete/92019/spam Del: non-english -- https://master.php.net/note/delete/92019/non-english Del: in docs -- https://master.php.net/note/delete/92019/in+docs Del: other reasons-- https://master.php.net/note/delete/92019 Reject -- https://master.php.net/note/reject/92019 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#157942) next »