Free PHP course. Sign Up for tracking progress →

PHP: Expressions in definitions

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:

<?php

$dollarsCount = 50 * 1.25;
print_r($dollarsCount);

Last lesson, we assigned a specific value to a variable. And here, in $dollarsCount = 50 * 1.25; there's an expression to the right of the equals sign. The interpreter calculates the result, 62.5, and records it in a variable. The interpreter doesn't care about what's in front of it: 62.5 or 50 * 1.25, both are expressions to compute. Calculating either of them will give 62.5.

Any string is an expression. Concatenating strings is also an expression. The interpreter processes whatever expression it encounters and produces, as a result, the value of the expression. Here are some sample expressions with comments on the right containing the resulting value:

<?php

62.5             // 62.5
50 * 1.25        // 62.5
120 / 10 * 2     // 24
(int) '100'      // 100

"hello"          // "hello"
"Good" . "will"  // "Goodwill"

The rules of code construction (or the grammar of the language) are such that in those places where an expression is expected, you can put any calculation (not only mathematical, but also, for example, string operations, e.g. concatenation) and the program will remain workable. For this reason, it's impossible to describe and show all use cases of all operations.

Programs consist of numerous combinations of expressions and understanding this concept is a major step on your journey.

Based on the above, consider whether this code will work.

<?php

$who = "dragon's" . 'mother';
print_r($who);

Run it on repl.it and experiment a little with it.

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

$yuansPerDollar = 6.91;
$dollarsCount = 50 * 1.25; // 62.5
$yuansCount = $dollarsCount * $yuansPerDollar; // 431.875

print_r($yuansCount);

Now, let's merge our output with some text via concatenation:

<?php

$yuansPerDollar = 6.91;
$dollarsCount = 50 * 1.25; // 62.5
$yuansCount = $dollarsCount * $yuansPerDollar; // 431.875

print_r('The price is ' . $yuansCount . ' yuans');
The price is 431.875 yuans

Any variable can be part of any expression. At the moment of calculation, its value is substituted for the variable name.

The interpreter calculates the value of $dollarsCount before this variable is used in other expressions. When it comes time to use a variable, PHP "knows" the value because it's already computed it.

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 yuans and displays the result on a new line.

Example output for 100 euros:

125
863.75

For the purposes of the exercise, we'll say that:
- 1 euro = $1.25
- 1 dollar = 6.91 yuans

The exercise doesn't pass checking. What to do? 😶

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.
In my environment the code works, but not here 🤨

Tests are designed so that they test the solution in different ways and against different data. Often the solution works with one kind of input data but doesn't work with others. Check the «Tests» tab to figure this out, you can find hints at the error output.

My code is different from the teacher's one 🤔

It's fine. 🙆 One task in programming can be solved in many different ways. If your code passed all tests, it complies with the task conditions.

In some rare cases, the solution may be adjusted to the tests, but this can be seen immediately.

I've read the lessons but nothing is clear 🙄

It's hard to make educational materials that will suit everyone. We do our best but there is always something to improve. If you see a material that is not clear to you, describe the problem in “Discussions”. It will be great if you'll write unclear points in the question form. Usually, we need a few days for corrections.

By the way, you can participate in courses improvement. There is a link below to the lessons course code which you can edit right in your browser.

Tips

  • You can use \n between the withdrawal of dollars and yuans to go to a new line.