-
Notifications
You must be signed in to change notification settings - Fork 92
/
description.en.yml
93 lines (58 loc) · 2.95 KB
/
description.en.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
---
name: Arguments as expressions
theory: |
Look at the code from the previous lesson:
```php
<?php
$result = round(10.25); // 10
```
The `round` function is invoked with the parameter `10.25`.
We already know that **expressions are converted into values**. It means that PHP treats values and expressions as something similar. Therefore technically, any value in a program can be replaced by an expression.
We can invoke a function with an **expression** as an argument:
```php
<?php
$result = round(8 + 2.25); // 10
```
The result will be the same as in the first example, because `8 + 2.25` will be converted into `10.25`, and `round` will be invoked with that value.
Besides, we can combine variables with values and other expressions:
```php
<?php
$number = 1.25;
$result = round($number + 7 + 2); // 10
```
Naturally, it works not only with numbers, but also with any values and expressions. For example, with strings.
As you remember, PHP has a pre-made function named `ucfirst`, that receives a string and returns a new string where the first letter is capitalized. Here is an example of a simple call of that function:
```php
<?php
$result = ucfirst('dragon'); // 'Dragon'
```
And here is an example of passing an expression to the same function:
```php
<?php
$result = ucfirst('dra' . 'gon'); // 'Dragon'
```
We will get the same result: `$result` will eventually contain the string `Dragon`, because the expression `dra` . `gon` will be evaluated as `dragon` and the function `ucfirst` will use it as an argument.
Let's sum it up. Look at some examples from the current lesson:
```php
<?php
// simple calls
$result = round(10.25); // 10
$result = ucfirst('dragon'); // 'Dragon'
// expressions as arguments
$result = round(8 + 2.25); // 10
$result = ucfirst('dra' . 'gon'); // 'Dragon'
// expressions with variables as arguments
$number = 1.25;
$result = round($number + 7 + 2); // 10
$text = 'dr';
$result = ucfirst($text . 'ag' . 'on'); // 'Dragon'
```
You have probably noticed the similarity: every time a function is invoked, we pass it some information. Sometimes it's a "ready-to-use" value (`10.25`, `dragon`), and sometimes it's a compound expression - a `raw` value (`8 + 2.25`, `$number + 7 + 2`, `$text . 'ag' . 'on'` etc.), with only one argument being passed in all examples. When a function takes several arguments, they are separated by comas.
instructions: |
You have access to the function `calculateDistanceBetweenTowns`. It receives one argument that contains two city names separated by a hyphen. The function returns distance between the two cities. Look at the example:
```php
<?php
$distance = calculateDistanceBetweenTowns('Lannisport-Bayasabhad');
```
Write a program that uses the function `calculateDistanceBetweenTowns` and print to screen distance between the cities assigned to variables `$from` and `$to`.
tips: []