phpstringvariablesphpmailer

print variable in string using php


I have try to replace short-code with php variable and try to print value of variable in string. Hope below code will give you a more idea.below is my code :

<?php
$string='hi i am [name] suthar and my email is [mail]. today i very happy because [message]';
$name="xyz";
$mail="xyz@xyz.com";
$message="my message"; 
$string=str_replace('[name]', '$name', $string ); 
$string=str_replace('[mail]', '$mail', $string ); 
$string=str_replace('[message]', '$message', $string ); 
echo $string; 
?>

What I want as an output :

hi i am xyz suthar and my email is xyz@xyz.com. today i very happy because my message.


Solution

  • $string='hi i am [name] suthar and my email is [mail]. today i very happy because [message]';
    $name="xyz";
    $mail="xyz@xyz.com";
    $message="my message"; 
    $string=str_replace('[name]', $name, $string ); 
    $string=str_replace('[mail]', $mail, $string ); 
    $string=str_replace('[message]', $message, $string ); 
    echo $string; 
    

    Why do you need single quote around the variable?