Robert_Craig Posted September 14, 2020 Share Posted September 14, 2020 I am trying to create a math equation inside a variable but I am struggling to achieve the desired result. On one php page I have this: $x = "date('Y')"; $y = "1989"; $description = "In this classic lecture which was delivered over $x - $y years ago, etc etc....."; On my main page I have linked to the above and also $description but the result I am getting is: In this classic lecture which was delivered over date('Y') - 1989 years ago, I want to create a variable so I don't need to go back every year and change the description. Is this possible? Robert Quote Link to comment https://forums.phpfreaks.com/topic/311485-php-math-equations/ Share on other sites More sharing options...
Barand Posted September 14, 2020 Share Posted September 14, 2020 You can't put functions inside strings like variables. <?php $x = date('Y'); $y = 1989; $description = "In this classic lecture which was delivered over " . ($x - $y) . " years ago, etc etc....."; echo $description; // ==> In this classic lecture which was delivered over 31 years ago, etc etc..... ?> 1 Quote Link to comment https://forums.phpfreaks.com/topic/311485-php-math-equations/#findComment-1581391 Share on other sites More sharing options...
Robert_Craig Posted September 14, 2020 Author Share Posted September 14, 2020 Thank you Barand for the quick reply and easy explanation. Quote Link to comment https://forums.phpfreaks.com/topic/311485-php-math-equations/#findComment-1581395 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.