Created
February 27, 2012 12:11
-
-
Save h4/1923334 to your computer and use it in GitHub Desktop.
PHP для курса dev-p14
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<h1>Синтаксис PHP</h1> | |
<p>PHP код может сожержать обычную html-разметку</p> | |
<p>Но чаще всего это код внутри специальных меток:</p> | |
<?php | |
// Здесь начинается php-код | |
// Код должен содержать комментарии | |
echo 'Hello'; | |
/* | |
Php не прощает ошибок в синтаксисе: | |
* Все строки должны быть в кавычках | |
* В конце каждой строки — точка с запятой | |
*/ | |
?> | |
<p>echo 'Hello' — это уже обычный html</p> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$myVar = 1; // правильное имя переменной | |
$my_another_var = 2; // тоже правильное имя переменной | |
$1_item = "Bug"; // неправильное имя переменной | |
$second var = "Yepp"; // неправильное имя переменной | |
echo $myVar * $my_another_var; // 2 | |
echo '<p>' . $myVar . $my_$my_another_var . '</p>'; // <p>12</p> | |
?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$inncome = 12234; // доход | |
$tax = 0.16; // ставка налога | |
$employers = 3; // число сотрудников | |
$salary = 534; // зарплата | |
$saldo = $income * (1 - $tax) - $employers * $salary; | |
if ($saldo > 0) { | |
echo '<p>Прибыльное дело</p>'; | |
} else { | |
echo '<p>Убыточное дело</p>'; | |
} | |
// Альтернативная запись | |
if ($saldo > 0) : | |
echo '<p>Прибыльное дело</p>'; | |
else : | |
echo '<p>Убыточное дело</p>'; | |
endif; | |
?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// While – цикл с условием. Выполняется до тех пор, пока условие истинно | |
$bugs_counter = 3; | |
function fix_bug($bugs) { | |
// Magick! | |
return $bugs--; | |
} | |
while ($bugs_counter) { | |
$bugs_counter = fix_bug($bugs_counter); | |
} | |
// Бесконечный цикл | |
function i_can_see_on_water() { | |
return true; | |
} | |
while (i_can_see_on_water()) { | |
echo '<p>Ohmmm</p>' | |
} | |
?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// for — цикл со счётчиком | |
$distance = 10000; | |
$lap = 400; | |
$runner = 'Tirunesh Dibaba'; | |
$laps = $distance / $lap; | |
function run_lap($runner, $lap) { | |
echo '<p>' . $runner . ' runs ' . $lap . '</p>'; | |
} | |
for ($i = 0; $i <= $laps; $i++) { | |
run_lap($runner, $i); | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment