Get the solution ↓↓↓
The issue is that your expression is
{-code-1}
First, it evaluates the leftmost operand, which is'+'. Then it appends$i to thatString, resulting in something like '+123'. And then it tries to numerically add1 to thatString, 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 yourString.
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'));
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'));
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.
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.