0

Is it possible to change the value of a string like this. See code below:

$name = 'Henk';
$str = "Hi $name how are you?";

$name = 'Kees';    
echo $str; // prints: Hi Henk how are you?

I want to print:

Hi Kees how are you?
Julian S
  • 361
  • 3
  • 13
  • 2
    You're probably looking for something more akin to [sprintf](http://php.net/sprintf). What you're implying code wise isn't possible as you can't bind variables (rather than values) to strings. – Jonnix Nov 11 '16 at 10:51

1 Answers1

2

You are looking for the sprintf function.

Take a look at http://php.net/manual/en/function.sprintf.php (example 1)


Thanks for the code. I rewrote the code like this:

$name = 'Henk';
$format = 'Hi, %s how are you';
echo sprintf($format, $name); //Hi, Henk how are you

$name = 'Kees';
echo sprintf($format, $name); //Hi, Kees how are you
Julian S
  • 361
  • 3
  • 13
Matt Backslash
  • 764
  • 1
  • 8
  • 20