Skip to content

Commit

Permalink
Merge pull request #350 from hexlet-basics/expressions-in-definitions
Browse files Browse the repository at this point in the history
Expressions in definitions
  • Loading branch information
fey committed May 5, 2023
2 parents 3b17c37 + 50f5332 commit 7548193
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 15 deletions.
2 changes: 1 addition & 1 deletion modules/30-variables/15-expressions/Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class Test extends TestCase
{
public function test()
{
$expected = "125\n7500";
$expected = "125\n863.75";
$this->expectOutputString($expected);
require 'index.php';
}
Expand Down
24 changes: 12 additions & 12 deletions modules/30-variables/15-expressions/description.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
name: Expressions in definitions
theory: |
Variables are helpful not only for storing and reusing data, but also for simplifying complex calculations. Consider an example: you need to convert euros into rubles through dollars. Banks often do this kind of conversion via an intermediate currency when shopping abroad.
Variables are helpful not only for storing and reusing data, but also for simplifying complex calculations. Consider an example: you need to convert euros into yuans through dollars. Banks often do this kind of conversion via an intermediate currency when shopping abroad.
First, convert 50 euros into dollars. Let's assume that one euro equals $1.25:
Expand Down Expand Up @@ -45,32 +45,32 @@ theory: |
Run it on [repl.it](https://repl.it/languages/php) and experiment a little with it.
Now, back to our currency program. We'll record the dollar value in rubles as a separate variable. Let's calculate the price of 50 euros in dollars by multiplying it by `1.25`. Let's say that 1 dollar is 60 rubles:
Now, back to our currency program. We'll record the dollar value in yuans as a separate variable. Let's calculate the price of 50 euros in dollars by multiplying it by `1.25`. Let's say that 1 dollar is 6.91 yuans:
```php
<?php
$rublesPerDollar = 60;
$yuansPerDollar = 6.91;
$dollarsCount = 50 * 1.25; // 62.5
$rublesCount = $dollarsCount * $rublesPerDollar; // 3750
$yuansCount = $dollarsCount * $yuansPerDollar; // 431.875
print_r($rublesCount);
print_r($yuansCount);
```
Now, let's merge our output with some text via concatenation:
```php
<?php
$rublesPerDollar = 60;
$yuansPerDollar = 6.91;
$dollarsCount = 50 * 1.25; // 62.5
$rublesCount = $dollarsCount * $rublesPerDollar; // 3750
$yuansCount = $dollarsCount * $yuansPerDollar; // 431.875
print_r('The price is ' . $rublesCount . ' rubles');
print_r('The price is ' . $yuansCount . ' yuans');
```
<pre class='hexlet-basics-output'>
The price is 3750 rubles
The price is 431.875 yuans
</pre>
Any variable can be part of any expression. At the moment of calculation, its value is substituted for the variable name.
Expand All @@ -79,7 +79,7 @@ theory: |
instructions: |
Write a program that takes the original number of euros from the `$eurosCount` variable, converts euros to dollars, and displays the value on the screen. Then the program converts the received value into rubles and displays the result on a new line.
Write a program that takes the original number of euros from the `$eurosCount` variable, converts euros to dollars, and displays the value on the screen. Then the program converts the received value into yuans and displays the result on a new line.
Example output for 100 euros:
Expand All @@ -90,8 +90,8 @@ instructions: |
For the purposes of the exercise, we'll say that:
- 1 euro = $1.25
- 1 dollar = 60 rubles
- 1 dollar = 6.91 yuans
tips:
- |
You can use `\n` between the withdrawal of dollars and rubles to go to a new line.
You can use `\n` between the withdrawal of dollars and yuans to go to a new line.
4 changes: 2 additions & 2 deletions modules/30-variables/15-expressions/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
$dollarsCount = $eurosCount * 1.25;
print_r($dollarsCount);
print_r("\n");
$rublesCount = $dollarsCount * 60;
print_r($rublesCount);
$yuansCount = $dollarsCount * 6.91;
print_r($yuansCount);
// END

0 comments on commit 7548193

Please sign in to comment.