r/PHPhelp • u/ashkanahmadi • Jul 30 '20
Is there any difference between echoing out HTML and constantly dropping in and out of PHP?
Hi
Imagine I have a small IF condition (if (2 ==2)) and I want to output 2 things based on the result. I can either echo out the HTML (code 1 below) or just drop in and out of PHP so there is no echoing (code 2 below).
Two issues I can think of is that Code 1 is more prone to errors (mistyping, missing closing or opening PHP tags, etc), and even though in this case Code 2 looks cleaner, writing a few hundred lines of HTML inside echo would be pretty messy.
Is there any advantage or disadvantage?
Thanks
Code 1 (in and out of PHP)
<?php
if (2==2) { ?>
<p>Same</p>
<?php } else { ?>
<p>Not the same</p>
<?php } Code 2 (always in PHP)
<?php
if (2==2) {
echo '<p>Same</p>';
} else {
echo '<p>Not the same</p>';
}18
u/kammysmb Jul 30 '20
For your sample #1 if you're using PHP on HTML templates, I would suggest using short syntax since it's cleaner (imo):
<?php if (2 == 2) : ?>
<p>Same</p>
<?php else : ?>
<p>Not the same</p>
<?php endif; ?>As for your question I would personally keep PHP and HTML separate, pure PHP echo is fine if you're just outputting a string, but once you're putting in symbols and tags, it's better to separate it so that you can get proper code highlighting from most editors/IDEs and also to make it easier to add/change things to the HTML.
8
u/halfercode Jul 30 '20
I think it's cleaner to drop in and out. I prefer this format, treating each bit of PHP as a little tag:
<?php if (2==2): ?>
<p>Same</p>
<?php else: ?>
<p>Not the same</p>
<?php endif ?>3
u/stfcfanhazz Jul 30 '20
I prefer this approach too if I had to choose. OP should be aiming for an MVC style pattern and a templating engine though!
7
u/Atulin Jul 30 '20
Once you go Twig, you never go back.
1
-1
u/llagerlof Jul 30 '20
Or Smarty.
1
u/stfcfanhazz Jul 30 '20 edited Jul 31 '20
If by that you mean you'll never get back to the future, marty!
3
u/ccrlop Jul 30 '20
Why does one use a templating engine line Twig? Does it affect performance?
Also i wonder if there is a performance increase using pure php to echo versus dropping in n out?
Thanks
2
u/Atulin Jul 30 '20
<h1>{{title}}</h1>is an easier syntax than
<h1><?= $title ?></h1>Not to mention additional features like custom filters and stuff. If you need to render some Markdown, for example, you can create a Twig filter and just do
<div>{{text | md}}</div>Far as performance goes, it gets transpiled to PHP in the process, so performance impact is negligible if any
1
u/amart1026 Aug 07 '20
The biggest benefit of using a template engine is that you can completely separate your PHP from your HTML. This means when you’re writing PHP you don’t have to worry about how it will be displayed. You simple build an object with all the info you’ll need and pass it to your template. The template decides how it will be displayed. This means you can switch templates with ease without touching you PHP logic. You can have lots of different looks for the same object.
3
u/owenmelbz Jul 30 '20
Option 1 always.
Let’s you write real HTML and don’t worry about escaping, new lines, you get code editor support etc.
Just don’t bother with the secondt way
1
u/nealio82 Jul 30 '20
The 2nd way is easier to read in this case (IMO), but functionally there's no difference. For stylistic problems like this it's often a case of just making a decision at the time which is more readable and less likely to confuse you when you come back to it in 6 months' time.
By the way, template engines such as Twig are designed to solve this problem for you, but might be overkill for what you need right now :D
In Twig you'd write
<p>{% if 2==2 %}Not the same{% else %}Same{% endif %}</p>
1
u/thebardingreen Jul 30 '20
Personally, if I was going to write 200 lines of HTML inside an echo, I would instead write
include("200lines.html");I personally hate reading code that tags in and out of PHP all the time. I would much rather separate my code into different files.
1
u/mangelsnc Aug 16 '20
Bro, don’t do that, use Twig or Blade or another templating engine, it worths it.
1
u/jpswade Aug 19 '20
PHP started life as a templating language, so dipping in and out of PHP like that is by design.
The pattern for this is called MVP, which means separating the model from the view and controller.
HTML is basically the view and you business logic belongs in the model. That’s the basic premise behind the concept anyway.
1
8
u/CyberJack77 Jul 30 '20
Like u/kammysmb said, try to separate your PHP and HTML. It's cleaner and it will make things much easier when your project grows.
There is no difference when looking at the output, however, there is a difference between echo and using the open/close signs.
Stopping PHP (
?>) will stop the PHP parser from parsing. It assumes everything after the closing sign (?>) is text that should simply be returned, until a open sign (<?php) is reached.Text displayed by using
echowill be parsed by PHP, depending on if the text is between a single or double quote. Text between a single quote will be seen as just text. Text between a double quote will be parsed, which allows you to echo the content of a variable.