Expressions in variable definition
Variables are useful not only for storing and reusing infomation, but also for simplifying complex calculations. Let’s look at the following example: we need to convert euro to rubles with dollars as intermediary currency. Such convertations are regularly done by banks when we shop abroad.
Let’s first convert 50 euro into dollars. Let’s say one euro is 1.25 dollars:
<?php $dollarsCount = 50 * 1.25; print_r($dollarsCount);
In the previous lesson we assigned a specific value to a variable. However in the code above
$dollarsCount = 50 * 1.25; we find an expression on the right side of the equal sign. The interpreter will calculate the result -
62.5 and will keep it behind that variable. From the interpreter’s point of view, it doesn’t matter whether it’s
50 * 1.25. It treats both as expressions that need to be evaluated. Having evaluated them, it produces the same result -
Every string represents an expression. String concatenation is an expression too. Whenever the interpreter sees an expression, it evaluates it and generates the result - the value of the expression. You will find some examples of expressions below with the final value of each expression in the comments:
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 building (grammar of the language) are such, that we can give a program a calculation (not only a mathematical one, but also a string calculation like concatenation) anywhere an expression is expected, and the program will remain workable. As a result, it’s impossible to describe and show every possible case of every possible operation.
Any program consists of a set of expression combinations and understanding of this concept is one of the key steps on you path to becoming a developer.
Based on all the above analyze the code below. Will it work?
<?php $who = "dragon's" . 'mother'; print_r($who);
Run it on repl.it and experiment.
Now back to our currency convertation program. We will create a separate variable and assign to it the value of one dollar in rubles. Now let’s calculate the dollar value of 50 euro multiplying it by
1.25. Let’s say 1 dollar is worth 60 rubles:
<?php $rublesPerDollar = 60; $dollarsCount = 50 * 1.25; // 62.5 $rublesCount = $dollarsCount * $rublesPerDollar; // 3750 print_r($rublesCount);
Now let’s add a text to the input using concatenation:
<?php $rublesPerDollar = 60; $dollarsCount = 50 * 1.25; // 62.5 $rublesCount = $dollarsCount * $rublesPerDollar; // 3750 print_r('The price is ' . $rublesCount . ' rubles');
The price is 3750 rubles
Any variable can be part of any expression. At the moment of its evaluation, the variable name is replaced by its value.
PHP interpreter calculates the value of
$dollarsCount before this variable is used in other expressions. Once it’s time to use that variable, PHP already “knows” the value because it has already calculated it.
Write a program that takes the given amount of euro assigned to the variable
$eurosCount, converts euro to dollars and outputs the result. Then it converts the result into rubles and prints it on a new line.
An output example for 100 euro:
Use the following rates:
- 1 euro = 1.25 dollars
- 1 dollar = 60 rubles
For line break use
\nbetween dollar and ruble output.
Exercise available only for signed users.
Please sign in with your GitHub account, this is necessary to track the progress of the lessons. If you do not have an account yet, now is the time to create an account on Github.