3

In the following snippet of PHP the $phrases needs to reflect the change made in $greeting. How can this be achieved?

$greeting = "Hello!";

$phrases = array('greeting' => $greeting . " Glad to see you.");
echo $phrases['greeting'];

Hello! Glad to see you.

$greeting = "How are you?";
echo $phrases['greeting'];

Hello! Glad to see you.

Note, that even after the value of $greeting variable changed, the array remained unchanged (which is normally an expected behavior, since the value of $greeting var is passed by value).

In order to make array to do change, I tried to use references to variables, but they don't seem to work with concatenation operator ..

Appreciate if anyone could suggest a quick solution to this..

2
  • 2
    I believe you cant do this the way you want, because the array gets 'built' up there. To reflect the change, you may have to write another function or something that can refresh the array contents. Commented Apr 10, 2011 at 10:07
  • How about having a class with greeting as a property ? may not be a quick solution though Commented Apr 10, 2011 at 10:12

5 Answers 5

2

most common way to add placeholders to your strings would be printf()

$phrases = array('greeting' => "%s Glad to see you.");

$greeting = "Hello!";
printf($phrases['greeting'],$greeting);
$greeting = "How are you?";
printf($phrases['greeting'],$greeting);
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks! Unfortunately, your solution only covers printing part. What if later we need to reassign $phrases['greeting'] to another variable e.g. $x = $phrases['greeting']?
OP wants the changes to be reflected in the array, when he changes $phrases['greeting'] in a (usual) separate statement.
Thrustmaster, exactly. I need the changes be seen in array.
0

You are trying to create a dynamic string variable, which is not possible in php.

You need to use a function.

A method which is probably closest to your code is storing as array with reference and using implode:

$greeting = "Hello!";

$phrases = array(&$greeting," Glad to see you.");
echo implode($phrases);

$greeting = "Hi!";
echo implode($phrases);

Edit: removed two other possible methods

1 Comment

Thanks, Billy. This is somewhat close to what I intended. However I'm still not sure how to best use it with a deep nested array.
0

Easy, use the &.

Example:

$a = 'a';
$b = array(&$a);
$a = 'b';
print_r($b);

Prints:

Array ( [0] => b )

Edit: You can not use the &-symbol when concatenating strings. You should redefine the whole variable or use printf or something (but I recommend you to redefine the whole variable.)

Comments

0

So you want that your value in the $pharses array changes everytime you change the value of $greeting? This is not possible, since when you create string, a "string-object" is created, that is a reference to a certain part in memory where the value of it is hardcoded and will never be changed.

You would have to write a function, like

function getPhrase(which) {
   // do concatenation using $greeting
}

which you have to call whenever you want to output a phrase.

Comments

0

A few years later.. You could acheave the same with:

$greeting = "Hello!";

$phrases = array('greeting' => function() use (&$greeting) {
                return $greeting." Glad to see you.";
            });

echo call_user_func($phrases['greeting']);

$greeting = "Oh NOOO!";
echo call_user_func($phrases['greeting']);

Comments

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.