PHP: Syntactic Sugar
Repetitive constructions come up often in programming. PHP, like many other languages, lets you write them more concisely. Such simplifications are called syntactic sugar, because they make the process of writing code a bit easier and more pleasant, "sweetening" it :)
Compound assignment forms
You often need to change the value of a variable by adding or subtracting something, or by multiplying or dividing it by a number. The basic version looks like this:
<?php
$index = $index + 1;
$count = $count * 2;
$total = $total - 5;
$price = $price / 3;PHP lets you write this more concisely using compound operators:
<?php
$index += 1; // the same as $index = $index + 1
$count *= 2; // the same as $count = $count * 2
$total -= 5; // the same as $total = $total - 5
$price /= 3; // the same as $price = $price / 3It is important to understand that the difference is solely in the way it is written. The interpreter turns the shortened construction into the expanded one.
In loops these shortcuts come up especially often. Inside them we usually change a counter and accumulate a result:
<?php
$sum = 0;
$index = 1;
while ($index <= 5) {
$sum += $index; // the same as $sum = $sum + $index
$index += 1; // the same as $index = $index + 1
}
print_r($sum); // => 15Without the shortcuts, the body of the loop would be longer:
<?php
while ($index <= 5) {
$sum = $sum + $index;
$index = $index + 1;
}Other operations
This notation works with numbers and with other data types.
For strings, the concatenation operator is used:
<?php
$text = 'Hello';
$text .= ' World'; // the same as $text = $text . ' World'Supported shortcuts
There is a shortened form for almost every operator: +=, -=, *=, /=, %=, **=, .=. They all work the same way: they take the current value of the variable, apply the operation, and store the result back in the same variable.
Instructions
Syntactic sugar operators are especially handy when you need to build up a final value step by step.
Implement the function buildProgressBar(), which takes the number of completed steps and the total number of steps, and then returns a progress indicator string.
Completed steps are marked with the # character, and the remaining ones with the - character. Try not to use built-in string functions in your solution.
<?php
buildProgressBar(0, 5); // '-----'
buildProgressBar(3, 5); // '###--'
buildProgressBar(5, 5); // '#####'You will find the .= operator useful for gradually building the new string inside a while loop. And the += operator will help you control the loop condition.
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: Syntactic Sugar
Repetitive constructions come up often in programming. PHP, like many other languages, lets you write them more concisely. Such simplifications are called syntactic sugar, because they make the process of writing code a bit easier and more pleasant, "sweetening" it :)
Compound assignment forms
You often need to change the value of a variable by adding or subtracting something, or by multiplying or dividing it by a number. The basic version looks like this:
<?php
$index = $index + 1;
$count = $count * 2;
$total = $total - 5;
$price = $price / 3;PHP lets you write this more concisely using compound operators:
<?php
$index += 1; // the same as $index = $index + 1
$count *= 2; // the same as $count = $count * 2
$total -= 5; // the same as $total = $total - 5
$price /= 3; // the same as $price = $price / 3It is important to understand that the difference is solely in the way it is written. The interpreter turns the shortened construction into the expanded one.
In loops these shortcuts come up especially often. Inside them we usually change a counter and accumulate a result:
<?php
$sum = 0;
$index = 1;
while ($index <= 5) {
$sum += $index; // the same as $sum = $sum + $index
$index += 1; // the same as $index = $index + 1
}
print_r($sum); // => 15Without the shortcuts, the body of the loop would be longer:
<?php
while ($index <= 5) {
$sum = $sum + $index;
$index = $index + 1;
}Other operations
This notation works with numbers and with other data types.
For strings, the concatenation operator is used:
<?php
$text = 'Hello';
$text .= ' World'; // the same as $text = $text . ' World'Supported shortcuts
There is a shortened form for almost every operator: +=, -=, *=, /=, %=, **=, .=. They all work the same way: they take the current value of the variable, apply the operation, and store the result back in the same variable.
Instructions
Syntactic sugar operators are especially handy when you need to build up a final value step by step.
Implement the function buildProgressBar(), which takes the number of completed steps and the total number of steps, and then returns a progress indicator string.
Completed steps are marked with the # character, and the remaining ones with the - character. Try not to use built-in string functions in your solution.
<?php
buildProgressBar(0, 5); // '-----'
buildProgressBar(3, 5); // '###--'
buildProgressBar(5, 5); // '#####'You will find the .= operator useful for gradually building the new string inside a while loop. And the += operator will help you control the loop condition.
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\SyntacticSugar;
use HexletBasics\Exercise\TestCase;
class SolutionTest extends TestCase
{
public function test()
{
$this->assertEquals('-----', buildProgressBar(0, 5));
$this->assertEquals('###--', buildProgressBar(3, 5));
$this->assertEquals('#####', buildProgressBar(5, 5));
}
}Teacher's solution will be available in:
20:00
