1

Is there a simple way to update the values of variables that are already present in strings (without using eval or replace functions) with the {$var} syntax?

In this example:

$id_a=1; $id_b = 2;
echo $str = "The id {$id_a} is related to id {$id_b}\n";
// operations go in here that calculate new values for $id_ variables
$id_a=124214; $id_b=325325;
echo $str = "The id {$id_a} is related to id {$id_b}\n";

You notice that I am assigning the same string twice to $str. My goal is to assign only once and every time I echo $str if $id_a or $id_b were changed, $str would have the updated values.

If there is a function that achieves this (even if it was not intended for specifically doing this), I haven't found it yet and I would be glad to know about it...

7
  • without using eval or replace What is wrong with replacing them? You somehow need a placeholder, which you can change out. For what else are you looking for? Commented Jun 11, 2015 at 17:52
  • @Rizier123 I gave a short example... The string I am dealing with has lots of variables, and assigning the whole string again is not an option. Commented Jun 11, 2015 at 17:53
  • Why would you need to assign it again, just assign it once with placeholder and replace them every time, before you print the string. I don't see why you can't use a placeholder, which you then can replace Commented Jun 11, 2015 at 17:54
  • 1
    Don't use a string, use a function. Commented Jun 11, 2015 at 17:56
  • 1
    Or maybe printf(). Commented Jun 11, 2015 at 17:59

2 Answers 2

3

Use sprintf to specify where the arguments need to appear in the string and pass $id_a and $id_b as parameters. E.g.

$id_a=1; $id_b = 2;
$format = "The id %d is related to id %d\n";
echo sprintf($format, $id_a, $id_b);
// operations go in here that calculate new values for $id_ variables
$id_a=124214; $id_b=325325;
echo sprintf($format, $id_a, $id_b);

This way you only declare your string's structure once and can re-use where-ever you need to output it. This also has the advantage of being able to convert your arguments into various formats (check the PHP docs page)

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

5 Comments

Well, I honestly didn't remember the printf option at all. Thanks dukelukem and AbraCadaver for mentioning it.
@Armfoot LOL, and you said, that you don't want to replace them... stackoverflow.com/questions/30787955/… 25 minutes ago already asked why not
@Rizier123 LOL I still don't get you Rizi... sprintf is reusing the $format variable, is that a hidden replacement happening somewhere? What about Barmar's answer? Care to detail?
@Armfoot Yep, that is what sprintf() or printf() are doing, you specify a placeholder, which you them can replace with some values. That's why I asked you right at the start, why you don't want to use replacement's.
@Rizier123 you could have written an answer with your placeholder solution, you were the first one to ask me details about it anyway... However I appreciate your generous -1 for making a point over such a serious matter. mmm's and duke's solutions were straight to the point and maybe they do operate in a similar way to str_replace, but I doubt it's heavier for the compiler and well... I won't be using a "replacement" after all ;)
1

with the idea of Barmar, it makes that :

function calculateString($id_a, $id_b) {
    return "The id {$id_a} is related to id {$id_b}\n";
}

$id_a=1; $id_b = 2;
echo $str = calculateString($id_a, $id_b);
$id_a=124214; $id_b=325325;
echo $str = calculateString($id_a, $id_b);

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.