1

Let's say I have an example like this one:

$foo = 'Hello ';
$bar = 1;

$abc =& $foo . $bar;

if (true) {
    ++$bar;

    if (true)
    {
        ++$bar;
    }
}

echo $abc;

I am expecting $abc to return Hello 3, but it actually returns Hello only. I'm really confused. Is there something I've gotten wrong with references in PHP?

5
  • where is $true && $true_truer defined? Commented Nov 11, 2013 at 21:52
  • 2
    A reference is a symbolic link to another variable - It can't keep track of multiple variables using a single reference. Commented Nov 11, 2013 at 21:52
  • $true is simply true Commented Nov 11, 2013 at 21:53
  • No, $true is not defined, so that entire if block is skipped. Commented Nov 11, 2013 at 21:54
  • Just assume it's true... Edited. Commented Nov 11, 2013 at 21:56

1 Answer 1

2

A reference variable is like an alias to the same object/variable, and it can only reference one variable at a time.

I'm not really sure how to help your situation because I don't know what you're trying to do, but..

$foo = 'Hello ';
$bar = 1;

$abc =& $bar;

++$bar;
++$bar;

echo $foo . $abc;

http://www.php.net/manual/en/language.references.whatdo.php

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

1 Comment

This is what I was looking for. Thank you! My situation is much more complex and the suggested solutions wont work, but all I needed to know was how the reference assignment operator works like and you pointed it out.

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.