Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Prodentim Probiotics Specially Designed For The Health Of Your Teeth And Gums

Instant Grammar Checker - Correct all grammar errors and enhance your writing

Teach Your Child To Read

Powerful WordPress hosting for WordPress professionals

Disclosure: My content contains affiliate links.

31,111 questions

40,781 answers

573 users

How to return a value by reference from a function in PHP

Learn & Practice SQL


121 views

image

asked Apr 19, 2014 by avibootz
edited Jun 9, 2014 by avibootz

1 Answer

0 votes
$arr = array("aaa", "bbb", "ccc", "ddd", "eee");
function &returnByRef($n) 
{
    global $arr;

    return $arr[$n];
}
$s =& returnByRef(2); // ccc
print_r($arr); // Array ( [0] => aaa [1] => bbb [2] => ccc [3] => ddd [4] => eee ) 
echo "
"; echo $s . "
"; // "ccc" $s = "ppp"; // changes $arr[2] to "ppp" echo $s . "
"; // "ppp" print_r($arr); // Array ( [0] => aaa [1] => bbb [2] => ppp [3] => ddd [4] => eee )




answered Apr 19, 2014 by avibootz
edited Jun 9, 2014 by avibootz

Related questions

...