How can I get the value of a variable written in string?

I have a php code

$string = "beautiful";
$time = "winter";

$str = 'This is a $string $time morning!';
echo $str;

It prints This is a $string $time morning!

How can get the value of variable in it?

Tagged:

Answers

  • You can use php function eval()

    <?php
    $string = "beautiful";
    $time = "winter";
    
    $str = 'This is a $string $time morning!';
    echo $str. "<br>";
    
    eval("\$str = \"$str\";");
    echo $str;
    ?>
    

    This will output
    This is a $string $time morning!
    This is a beautiful winter morning!

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!