Files
freeCodeCamp/guide/russian/php/loop/index.md

166 lines
3.3 KiB
Markdown
Raw Normal View History

2018-10-12 16:00:59 -04:00
---
title: Loop
2019-04-09 09:44:06 +03:00
localeTitle: Цикл
2018-10-12 16:00:59 -04:00
---
2019-04-09 09:44:06 +03:00
# PHP Цикл
2018-10-12 16:00:59 -04:00
Если вам нужно многократно повторять одну и ту же задачу, вы можете использовать цикл, а не добавлять один и тот же код снова и снова. В PHP есть следующие инструкции цикла:
* for - цикл через блок кода с определенным количеством раз.
* while - цикл через блок кода, если условие истинно.
* do ... while - цикл через блок кода один и продолжить цикл, если условие истинно.
* foreach - цикл через блок кода для каждого значения внутри массива.
Использование `break` внутри цикла может остановить выполнение цикла.
2019-04-09 09:44:06 +03:00
# Цикл For
2018-10-12 16:00:59 -04:00
2019-04-09 09:44:06 +03:00
Выполняет блок кода с определенным количеством раз.
2018-10-12 16:00:59 -04:00
## Синтаксис
```
for (init counter; condition; counter increment or decrement)
{
// Code to be executed
}
```
2019-04-09 09:44:06 +03:00
## Пример
2018-10-12 16:00:59 -04:00
```php
<?php
for($index = 0; $index < 5; $index ++)
{
echo "Current loop counter ".$index.".\n";
}
?>
```
## Вывод
```
> Current loop counter 0.
> Current loop counter 1.
> Current loop counter 2.
> Current loop counter 3.
> Current loop counter 4.
```
2019-04-09 09:44:06 +03:00
# Цикл While
2018-10-12 16:00:59 -04:00
2019-04-09 09:44:06 +03:00
Выполняет блок кода, если условие истинно.
2018-10-12 16:00:59 -04:00
## Синтаксис
```
while (condition)
{
// Code to be executed
}
```
2019-04-09 09:44:06 +03:00
## Пример
2018-10-12 16:00:59 -04:00
```php
<?php
$index = 10;
while ($index >= 0)
{
echo "The index is ".$index.".\n";
$index--;
}
?>
```
## Вывод
```
> The index is 10.
> The index is 9.
> The index is 8.
> The index is 7.
> The index is 6.
> The index is 5.
> The index is 4.
> The index is 3.
> The index is 2.
> The index is 1.
> The index is 0.
```
2019-04-09 09:44:06 +03:00
# Цикл Do ... While
2018-10-12 16:00:59 -04:00
2019-04-09 09:44:06 +03:00
Гарантированно выполняет первую итерацию цикла и продолжает выполняться, если условие истинно.
2018-10-12 16:00:59 -04:00
## Синтаксис
```
do
{
// Code to be executed
}
while (condition);
```
2019-04-09 09:44:06 +03:00
## Пример
2018-10-12 16:00:59 -04:00
```php
<?php
$index = 3;
do
{
// execute this at least 1 time
echo "Index: ".$index.".\n";
$index --;
}
while ($index > 0);
?>
```
## Вывод
```
> Index: 3.
> Index: 2.
> Index: 1.
```
# Цикл Foreach
2019-04-09 09:44:06 +03:00
Выполняет блок кода для каждого значения в массиве.
2018-10-12 16:00:59 -04:00
## Синтаксис
```
foreach ($array as $value)
{
// Code to be executed
}
```
2019-04-09 09:44:06 +03:00
## Пример
2018-10-12 16:00:59 -04:00
```php
<?php
$array = ["Ali", "Ah Kao", "Muthu", "Gwen", "Lucida", "Cecily", "Arthur", "Flora"];
foreach ($array as $name)
{
echo "Hi, my name is ".$name.".\n";
if ($name == "Cecily")
{
echo "\"Hello, ".$name."!\"";
// stop the loop if name is Cecily
break;
}
}
?>
```
## Вывод
```
> Hi, my name is Ali.
> Hi, my name is Ah Kao.
> Hi, my name is Muthu.
> Hi, my name is Gwen.
> Hi, my name is Lucida.
> Hi, my name is Cecily.
> "Hello, Cecily!"
2019-04-09 09:44:06 +03:00
```