In the expression echo "Você nasceu em ". date('Y') - 20
, you are subtracting the string concatenation with the date.
For you to understand better, it would be the same thing for you to do it:
echo "Você nasceu em 2017" - 20;
Therefore it is important to use the parentheses. For in such cases, the expression is first processed within the parentheses and then concatenated.
Tip: In cases like this, I usually use the function sprintf
to better format the string:
echo sprintf("Você nasceu em %d", date('Y') - 20);
See a test on IDEONE
Note: Depending on the version of PHP you are using (I believe from version 7 onwards), you may get an error if you do an operation like this mentioned in the question:
A non-numeric value encountered
I got it, in case he picks up the string too. I thought he only solved the operation I did. But then I got it, thank you Isac! And thanks to the others too!
– Lucas de Carvalho