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?
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?
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