- Основы синтаксиса
- Типы
- Переменные
- Константы
- Выражения
- Операторы
- Управляющие конструкции
- Функции
- Классы и объекты
- Пространства имен
- Errors
- Исключения
- Generators
- Ссылки. Разъяснения
- Предопределённые переменные
- Предопределённые исключения
- Встроенные интерфейсы и классы
- Контекстные опции и параметры
- Поддерживаемые протоколы и обработчики (wrappers)
Коментарии
As with echo, you can define a variable like this:
<?php
$text = <<<END
<table>
<tr>
<td>
$outputdata
</td>
</tr>
</table>
END;
?>
The closing END; must be on a line by itself (no whitespace).
[EDIT by danbrown AT php DOT net: This note illustrates HEREDOC syntax. For more information on this and similar features, please read the "Strings" section of the manual here: language.types.string ]
References and "return" can be flakey:
<?php
// This only returns a copy, despite the dereferencing in the function definition
function &GetLogin ()
{
return $_SESSION['Login'];
}
// This gives a syntax error
function &GetLogin ()
{
return &$_SESSION['Login'];
}
// This works
function &GetLogin ()
{
$ret = &$_SESSION['Login'];
return $ret;
}
?>
As an addendum to David's 10-Nov-2005 posting, remember that curly braces literally mean "evaluate what's inside the curly braces" so, you can squeeze the variable variable creation into one line, like this:
<?php
${"title_default_" . $title} = "selected";
?>
and then, for example:
<?php
$title_select = <<<END
<select name="title">
<option>Select</option>
<option $title_default_Mr value="Mr">Mr</option>
<option $title_default_Ms value="Ms">Ms</option>
<option $title_default_Mrs value="Mrs">Mrs</option>
<option $title_default_Dr value="Dr">Dr</option>
</select>
END;
?>