K
K
koi com2014-12-12 21:05:53
PHP
koi com, 2014-12-12 21:05:53

How to render a page using php?

Just started with php. How to render a page using php? Why not write echo '%long html code%' ? How is it done well?

Answer the question

In order to leave comments, you need to log in

7 answer(s)
F
FanatPHP, 2014-12-12
@FanatPHP

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>
...

2. If in another file - then readfile:
<?php
echo "PHP";
readfile('html.htm');

K
kstyle, 2014-12-12
@kstyle

www.sql.ru/forum/415817/vstavka-html-fayla-v-stranicu

N
Nur-Magomed, 2014-12-12
@efive

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;
?>

S
Sergey, 2014-12-12
@gangstarcj

<?php 
//какой-то код на php например
$s = 1;
if ($s === 1) {
?>
<div>С равно одному</div>
<?php } ?>

Here is another exemplary example
<?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?>

B
Boris Benkovsky, 2014-12-12
@benbor

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>

Z
zugo, 2014-12-14
@zugo

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.

K
Kirby, 2015-01-01
@andKirby

//in .phtml file
<div>
    <span>My Code</span>
    <?php if ($value) : ?>
        <span>Value: <?php echo $value ?></span>
    <?php endif; ?>
</div>

Take a PHP textbook and learn from it... It's strange that you ask such questions.
Once you're comfortable, try the Zend Framework (or whatever) and don't ask questions like that. ;) The framework will teach you MVC.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question