Python week November Black Friday
This week up to 80% off on Python courses. More info
Black friday is here! Get up to 80 % extra points for free! More info

Discussion: Problem with references

Activities (2)
Avatar
Paul Michaels
Member
Avatar
Paul Michaels:11/11/2016 13:51

This isn't working, and I think I don't completely understand references...

<?php
function add($array, $item)
{
        &$array[] = $item;
}

$a = array(1, 2, 3);
add($a, 4);
print_r($a);

?>
 
Reply 11/11/2016 13:51
Avatar
Replies to Paul Michaels
Ondřej Štorc:11/12/2016 4:26

You must use & in function declaration , like this:

<?php
function add(&$array, $item)
{
        $array[] = $item;
}

$a = array(1, 2, 3);
add($a, 4);
print_r($a);

?>
 
Up Reply 11/12/2016 4:26
To maintain the quality of discussion, we only allow registered members to comment. Sign in. If you're new, Sign up, it's free.

2 messages from 2 displayed.