Search code examples
phpechoinline-code

execute php code within string


This question is just for me to learn from. I can easily resolve it outside of the echo...but there MUST be a way to perform a basic calculation or function call within an echo statement...right?

$CurrentMembershipYear = '2016';

echo '<h2>Two-Year Membership for '.$CurrentMembershipYear.' and '.intval($CurrentMembershipYear)+1.'</h2>'; 

This should be echoing "Two-Year Membership for 2016 and 2017" but instead is generating errors. Usually I would just pre-calculate the second value before the echo statement and just pass it as a variable...but surely there is a way to put this calculation inline?


Solution

  • never mind...bone-headed brain asleep. Just needed parenthesis...I had only tried curvy braces:

    $CurrentMembershipYear = '2016';
    
    echo '<h2>Two-Year Membership for '.$CurrentMembershipYear.' and '.(intval($CurrentMembershipYear)+1).'</h2>';