1

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?

2
  • 3
    Put some parentheses around it. (Look up operator precedence for more info) Commented Nov 21, 2015 at 22:21
  • thanks @Rizier123...I figured it out right after I had given up and posted... but yes... Thank you. Commented Nov 21, 2015 at 22:25

2 Answers 2

1

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>';
Sign up to request clarification or add additional context in comments.

3 Comments

side note, you dont need intval
I added that because my numeric year is actually stored as a string not a number...so I thought that might have been the issue initially. I will give it a test without the intval and see what happens. Thanks.
its php, its loosely typed, so things like that work (they wouldn't in other languages)
0

parenthesis should solve it

(intval($CurrentMembershipYear)+1)

1 Comment

parenthesis should solve it yes, if you place them right.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.