PHP: Increment and decrement
Two operations are carried over from C to PHP:
- Increment
++ - Decrement
--
They are very common with loops. These unary operations increment and decrement by one the number written in a variable:
<?php
$i = 0;
$i++; // 0
$i++; // 1
$i--; // 2
$i--; // 1When using the prefix form, it's the other way around. First the variable is changed, and then the new value is returned:
<?php
$i = 0;
++$i; // 1
++$i; // 2
--$i; // 1
--$i; // 0It seems there's no difference between postfix and prefix forms. But this is where it gets complicated. All other operations have no side effects and simply return a new value. Unlike them, increment and decrement not only return a value but also change the value of the variable.
When using prefix notation, the variable is changed first, and then it's returned. When using postfix notation, it's the other way around: first it's returned, and then the variable is changed.
The rule works exactly the same for both increment and decrement. For simplicity, let's consider only increment:
<?php
$x = 5;
echo ++$x; // => 6
echo $x; // => 6
echo $x++; // => 6
echo $x; // => 7Let's discuss what happens step by step in the code:
- We printed
++$x— this is a prefix increment, so first the value of the variable is increased by 1, then the result is returned and printed to the screen - The value has changed, so
echo $xprinted 6 - Now we print
$x++— this is a postfix increment, so first the value is returned and printed to the screen, and then the variable is increased by 1 - The value has changed, so
echo $xprinted 7
Increment and decrement can significantly complicate the code. It's especially difficult when we insert increment inside other operations: $x = $i++ - 7 + --$h.
It's impossible to understand such code, so it's better not to use such constructions. For example, in JavaScript, when checking the code, the linter immediately starts to complain when it sees the use of increment and decrement.
We advise using these constructions as follows:
- Within a single expression, never mix functions without side effects with functions with side effects (the same applies to operations)
- Use increment and decrement only where there's no difference between the prefix and postfix variant: separately from everything, on its own line of code
Instructions
Increment and decrement are not very important operations in PHP, and you can easily manage without them. The assignment in this lesson is not directly related to this topic, but you can use increment and decrement to get used to them. Otherwise, this exercise is just more practice with loops, strings, and conditions.
Write a function, makeItFunny(), which returns a copy of a string passed to it, where each nth element is uppercase. n also needs to be passed to the function.
To find each nth element, you will need to find the remainder from division %. Think about how you can use it.
<?php
$text = 'I never look back';
// Every third element
makeItFunny($text, 3); // 'I NevEr LooK bAck'If you've reached a deadlock it's time to ask your question in the «Discussions». How ask a question correctly:
- Be sure to attach the test output, without it it's almost impossible to figure out what went wrong, even if you show your code. It's complicated for developers to execute code in their heads, but having a mistake before their eyes most probably will be helpful.
PHP: Increment and decrement
Two operations are carried over from C to PHP:
- Increment
++ - Decrement
--
They are very common with loops. These unary operations increment and decrement by one the number written in a variable:
<?php
$i = 0;
$i++; // 0
$i++; // 1
$i--; // 2
$i--; // 1When using the prefix form, it's the other way around. First the variable is changed, and then the new value is returned:
<?php
$i = 0;
++$i; // 1
++$i; // 2
--$i; // 1
--$i; // 0It seems there's no difference between postfix and prefix forms. But this is where it gets complicated. All other operations have no side effects and simply return a new value. Unlike them, increment and decrement not only return a value but also change the value of the variable.
When using prefix notation, the variable is changed first, and then it's returned. When using postfix notation, it's the other way around: first it's returned, and then the variable is changed.
The rule works exactly the same for both increment and decrement. For simplicity, let's consider only increment:
<?php
$x = 5;
echo ++$x; // => 6
echo $x; // => 6
echo $x++; // => 6
echo $x; // => 7Let's discuss what happens step by step in the code:
- We printed
++$x— this is a prefix increment, so first the value of the variable is increased by 1, then the result is returned and printed to the screen - The value has changed, so
echo $xprinted 6 - Now we print
$x++— this is a postfix increment, so first the value is returned and printed to the screen, and then the variable is increased by 1 - The value has changed, so
echo $xprinted 7
Increment and decrement can significantly complicate the code. It's especially difficult when we insert increment inside other operations: $x = $i++ - 7 + --$h.
It's impossible to understand such code, so it's better not to use such constructions. For example, in JavaScript, when checking the code, the linter immediately starts to complain when it sees the use of increment and decrement.
We advise using these constructions as follows:
- Within a single expression, never mix functions without side effects with functions with side effects (the same applies to operations)
- Use increment and decrement only where there's no difference between the prefix and postfix variant: separately from everything, on its own line of code
Instructions
Increment and decrement are not very important operations in PHP, and you can easily manage without them. The assignment in this lesson is not directly related to this topic, but you can use increment and decrement to get used to them. Otherwise, this exercise is just more practice with loops, strings, and conditions.
Write a function, makeItFunny(), which returns a copy of a string passed to it, where each nth element is uppercase. n also needs to be passed to the function.
To find each nth element, you will need to find the remainder from division %. Think about how you can use it.
<?php
$text = 'I never look back';
// Every third element
makeItFunny($text, 3); // 'I NevEr LooK bAck'If you've reached a deadlock it's time to ask your question in the «Discussions». How ask a question correctly:
- Be sure to attach the test output, without it it's almost impossible to figure out what went wrong, even if you show your code. It's complicated for developers to execute code in their heads, but having a mistake before their eyes most probably will be helpful.
Your exercise will be checked with these tests:
<?php
namespace HexletBasics\Loops\Mutators;
use HexletBasics\Exercise\TestCase;
class SolutionTest extends TestCase
{
public function test()
{
$text = 'I never look back';
$this->assertEquals('I NevEr LooK bAck', makeItFunny($text, 3));
$this->assertEquals('hElLo', makeItFunny('hello', 2));
}
}Teacher's solution will be available in:
20:00
