Search code examples
phpvariablesevaluate

Simple PHP stuff : variable evaluation


OK, I'm sorry if this question has been asked before, but I don't know the keyword to search for it. The situation is :

$sentence = ' $subject is a genius ';
$subject = " Einstein ";
echo( $sentence );

How can I make it echo "Einstein is a genius" but keep defining variables $sentence before defining $subject ?

You might realize that this is the basic concept of using template. Yes, I'm trying to achieve it. Thanks guys


Solution

  • Try using sprintf:

    $sentence = '%s is a genius';
    $subject  = 'Einstein';
    echo sprintf($sentence, $subject);
    

    This will output Einstein is a genius