1

I want to assign a value to an array element, which is returned by a function with a reference argument. The code:

function f(&$y) {
    return $y;
}

$y = '';

$x = array(
        'a' => 1,
        'b' => 2,
        'c' => f($y)
        );
var_dump($x); // 'c' is empty

$y = 3;

var_dump($x); // 'c' is still empty

I'm pretty sure I got the whole reference thing all wrong in this case. Still not working even if I try to return by reference like function &f(&y) {}.

3
  • 2
    References are not pointers. Commented Apr 11, 2013 at 20:00
  • The function f() returns the value of $y, and this is set in the $x array. The second assignment of $y does not affect this at all. But is this theory crafting or are you actually trying to achieve something? Commented Apr 11, 2013 at 20:00
  • You need return by reference as well. Commented Apr 11, 2013 at 20:04

4 Answers 4

3

If you want this with a function call (you could as well alias $y directly), then you would need to use return by reference (see: Returning References), see the & sign before the function name:

function &f(&$y) {
    return $y;
}

Also you need to alias that reference (which needs some special treatment to make the PHP parser allow this):

$y = '';

$x = array(
        'a' => 1,
        'b' => 2,
        'c' => &${${''}=&f($y)?!1:!1},
);

See it in action.

Sign up to request clarification or add additional context in comments.

Comments

2

You should try

$x = array(
    'a' => 1,
    'b' => 2,
    'c' => &$y
    );

1 Comment

Nope, the function does something else than returning the plain value of $y
1

First of all, a warning: this kind of code is way too confusing and you should only do it if threatened at gunpoint. Your future self will hate you when they have to debug code like this.

That said, to make this work you need "moar references":

function &f(&$y) { // two ref-signs here...
    return $y;
}

$y = '';
$x = array(
    'a' => 1,
    'b' => 2,
    'c' => &f($y) // ..and one more here
);

However, the above still does not compile (I have no idea why, will investigate when time permits) because it seems that you are not allowed to do that when in the middle of creating an array. But this works:

$y = '';
$x = array('a' => 1, 'b' => 2);
$x['c'] = &f($y); // seems this guy needs to be alone

$y = 3;
print_r($x);

See it in action.

4 Comments

That is because of the PHP parser. Technically it is possible: 'c' => &${${''}=&f($y)?!1:!1},.
@M8R-1jmw5r: I was afraid it would be something like this because PHP (obviously there is no technical reason this wouldn't fly). Frankly, I wouldn't be caught dead shipping that kind of code but +1 for effort.
Thank you, and exactly, I couldn't imagine as well why it did not work.
Right, that should've worked, instead it throws a fatal error Can't use function return value in write context. Jon, believe me, my future self will hate me anyway. What I need is to give that 'c' a strtotime return reference, but since I cannot pass arguments by reference anymore I was searching for other options. Thanks anyway, both of you.
-1

According to a quick glance at the PHP manual, it looks like your array() syntax is missing a comma.

$x = array(
    'a' => 1,
    'b' => 2,
    'c' => f($y)
    );

Should actually be

$x = array(
    'a' => 1,
    'b' => 2,
    'c' => f($y),
    );

Source: http://php.net/manual/en/language.types.array.php

I haven't written PHP in years, I'm just going by the example the author put.

1 Comment

WRONG. The comma is optional, not necessary.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.