php 7.4 - The behavior of unparenthesized expressions containing both '.' and '+'/'-' will change in PHP 8: '+'/'-' will take a higher precedence

Solution:

The issue is that your expression is

{-code-1}

First, it evaluates the leftmost operand, which is '+'. Then it appends $i to that String, resulting in something like '+123'. And then it tries to numerically add 1 to that String, but that will fail, because PHP cannot convert the lefthand-side to a number. In order to solve it, you will need to evaluate $i + 1 first and then evaluate your String.

You can do so implicitly:

$date1 = date('d M Y', strtotime('+' . ($i + 1) . 'month'));

or explicitly:

$monthOffset = $i + 1;
$date1 = date('d M Y', strtotime('+' . $monthOffset . 'month'));

Answer

Solution:

It appears you are using PHP 7.4, which marks certain things as deprecated. These are not errors. Manually add parentheses where appropriate.

Example:

$response = '1234567890';
$header = 'Content-length: ' . strlen($response) + 1;
header($header);

In PHP 5.6, $header would contain 1. In PHP 7.0 to 7.4, it would contain the same, but will also emit a deprecation warning. In PHP 8.0+, it will contain Content-length: 11.

We wrap the arithmetic expression in parentheses, so that to get the correct number is concatenated:

$response = '1234567890';
header('Content-length: ' . (strlen($response) + 1));

So, you can do so implicitly:

$date1 = date('d M Y', strtotime('+' . ($i + 1) . 'month'));

Answer

Solution:

PHP is complaining because what you wrote doesn't mean what you think it does, prior to PHP 8. (And because in most instances the code will behave incorrectly, though in your instance it works fine.)

As of PHP 8, it does mean what you think it does, but you might as well add parentheses anyway to make it clear.

$date1 = date('d M Y', strtotime('+' . $i + 1 . 'month'));
// prior to PHP 8, . and + have the same precedence, so this means:
$date1 = date('d M Y', strtotime((('+' . $i) + 1) . 'month'));
// for example, with $i=1:
$date1 = date('d M Y', strtotime(('+1' + 1) . 'month'));
$date1 = date('d M Y', strtotime(2 . 'month'));
$date1 = date('d M Y', strtotime('2month'));

However, you are seeing your code work just fine, because strtotime (at least as of PHP 7.4) doesn't care whether it sees '+2month' (as intended) or '2month'. The result of strtotime will be the same, and so $date1 will also be the same.

The deprecation warning doesn't really apply to you, because the overall result will not change when the precedence of these operators does. But to silence the warning (and any confusion) you can always change it by adding parentheses, as the other answers have suggested.

Source