-
Notifications
You must be signed in to change notification settings - Fork 92
/
description.ru.yml
62 lines (40 loc) · 2.31 KB
/
description.ru.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
---
name: Окружение
theory: |
Какое значение окажется внутри переменной `$result` после выполнения кода?
```php
<?php
$age = 5;
function generate()
{
return $age + 3;
}
$result = generate();
```
Правильный ответ: код упадёт с ошибкой. Потому что внутри функции нет переменной с именем `$age`, но функция пытается её использовать.
Функция изолирована от внешних переменных. Все переменные, определенные снаружи функции, никак не связаны с переменными, определенными внутри. В теле функции находится программа, изолированная от внешней среды (внешнего окружения).
Рассмотрим другой пример:
```php
<?php
$age = 5;
function changeAge()
{
$age = 8;
}
changeAge();
print_r($age); // => 5
```
Переменная, созданная внутри функции `changeAge()`, никак не влияет на внешнюю переменную `$age`, поэтому после вызова функции значение внешней `$age` не изменилось и осталось `5`.
instructions: |
Это задание не связано напрямую с уроком, это просто ещё одно полезное упражнение по работе с функциями.
Напишите функцию `getAgeDifference()`, которая принимает два года рождения и возвращает строку с разницей в возрасте в виде `The age difference is 11`. Например:
```php
<?php
$actual = getAgeDifference(2001, 2018);
print_r($actual); // => The age difference is 17
$actual2 = getAgeDifference(2020, 2002);
print_r($actual2); // => The age difference is 18
```
tips:
- |
Напомним, что в PHP есть функция `abs()`, которая возвращает модуль переданного числа: например, `abs(-12)` вернёт `12`.