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

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,895 questions

51,826 answers

573 users

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

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

1 answer 168 views
1 answer 160 views
2 answers 224 views
1 answer 120 views
1 answer 133 views
133 views asked Jul 16, 2016 by avibootz
...