Answer the question
In order to leave comments, you need to log in
Answer the question
In order to leave comments, you need to log in
All answers are confusing.
Although in fact everything is very simple.
If HTML is written in the same file, then stupidly close the PHP tag and write HTML as is
<?php
echo "PHP";
?>
<h1>HTML is the King!</h1>
<ul>
...
<?php
echo "PHP";
readfile('html.htm');
If without templates and you want everything in one file, then, as an option:
<?php
//здесь какой-то php-код выполняет разные действия
$a = 5;
//далее выводим html
echo<<<html
<!--Здесь HTML, при желании можно сюда вставлять значения php переменных, вот так: {$a} -->
html;
?>
<?php
//какой-то код на php например
$s = 1;
if ($s === 1) {
?>
<div>С равно одному</div>
<?php } ?>
<?if (!empty($arResult)):?>
<ul class="menu__list">
<?
foreach($arResult as $arItem):
if($arParams["MAX_LEVEL"] == 1 && $arItem["DEPTH_LEVEL"] > 1)
continue;
?>
<?if($arItem["SELECTED"]):?>
<li class="menu__item state--active"><a href="<?=$arItem["LINK"]?>" class="menu__link"><?=$arItem["TEXT"]?></a></li>
<?else:?>
<li class="menu__item"><a href="<?=$arItem["LINK"]?>" class="menu__link"><?=$arItem["TEXT"]?></a></li>
<?endif?>
<?endforeach?>
</ul>
<?endif?>
It's best to use templates. But they are hard to set up if you are new to PHP. So it's best to just separate the logic into an HTML template and business logic. for example
<?php // main file, index.php
$i = 0;
...
// какая-то лигика здесь, и подготовка переменных для вывода на HTML
include(__DIR__.'template.phtml'); //внутри файла будет доступна $i
<!-- template file, template.phtml -->
<html>
<body>i = <?= $i =></body>
</html>
Just put the HTML code in a separate *.php or *.phtml file and include it in your script with the include("yourfile.phtml") or require("yourfile.phtml") function.
//in .phtml file
<div>
<span>My Code</span>
<?php if ($value) : ?>
<span>Value: <?php echo $value ?></span>
<?php endif; ?>
</div>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question