Is there harm in outputting html vs. using echo? - php

I have no idea really how to say this, but I can demonstrate it:
<?php
if (true) {
echo "<h1>Content Title</h1>";
}
?>
vs
<?php if (true) { ?>
<h1>Content Title</h1>
<?php } ?>
What differences are there between the two? Will there be problems caused by not using echo? It just seems super tedious to write echo "html code"; all the time, specially for larger segments of html.
Also, bonus kudos to someone who can rephrase my question better. :)

There's a small difference between the two cases:
<?php
if (true) {
echo "<h1>Content Title</h1>";
}
?>
Here, because you're using double quotes in your string, you can insert variables and have their values rendered. For example:
<?php
$mytitle = 'foo';
if (true) {
echo "<h1>$mytitle</h1>";
}
?>
Whereas in your second example, you'd have to have an echo enclosed in a php block:
<?php if (true) { ?>
<h1><?php echo 'My Title'; ?></h1>
<?php } ?>
Personally, I use the same format as your second example, with a bit of a twist:
<?php if (true): ?>
<h1><?php echo $mytitle; ?></h1>
<?php endif; ?>
I find that it increases readability, especially when you have nested control statements.

There are no relevant differences performance-wise. But you won't be able to use variables in the latter, and it looks less clean IMO. With large blocks, it also becomes extremely difficult to keep track of {} structures.
This is why the following alternative notation exists:
<?php if (true): ?>
<h1>Content Title</h1>
<?php endif; ?>
it's marginally more readable.

This:
<?php if (true) : ?>
<h1>Content Title</h1>
<?php endif; ?>
This is the way PHP is supposed to be used.
Don't echo HTML. It's tedious, especially because you have to escape certain characters.
There may be slight performance differences, with the above probably being faster. But it shouldn't matter at all. Otherwise, no differences.

I appreciate all the feedback. :)
I did find out some issues when using the two different methods.
There does not appear to be any real issue here except that the formatting looks terrible on the source and the tedious nature of it.
<?php
if (true) {
echo "<h1>Content Title</h1>";
}
?>
Using php this way can cause an error as such
Warning: Cannot modify header information - headers already sent
<?php if (true) { ?>
<h1>Content Title</h1>
<?php } ?>
The headers error can possibly be solved by using php like so
<?php ob_start(); if (true) { ?>
<h1>Content Title</h1>
<?php } ob_end_flush(); ?>
As to why and when the headers are sent, I am not completely sure...

Related

open&close php tags on each line, within arrays, functions

I collaborate with a web-programmer on a php project based in kirby cms, and he wants to open and close every line as such:
<main>
<?php /*php code here/* ?>
<?php /*more php here*/ ?>
...
Trying to follow this style, I found some errors in my code. The first is that it seems I canNOT do this in the middle of an array as such:
BAD CODE
<?php $oo = array( ?>
<?php 'h' => 100, ?>
<?php 'v' => 100, ?>
<?php ); ?>
but I can do it in the middle of a foreach loop as such:
<?php foreach ($p as $subp): ?>
<div id='<?= $subp->title() ?>'>
<?php endforeach; ?>
Are there any other cases such as array in which I canNOT do this?
/edit
According to the answer, there can only be tag-breaks within 'foreach', 'while', or 'if' blocks.
How about a 'foreach', 'while' or 'if' within a function? is that 'legal'?:
<?php
function myFunction($arg){
if($arg === 'this'): ?>
<?= '<p>yep</p>' ?>
<?php else: ?>
<?= '<p>nop</p>' ?>
<?php endif;
};
?>
And how about nesting if within foreach within function?:
<?php
function myFunction($arr){
foreach($arr as $val): ?>
<p><?= $val ?> <p>
<?php if($val === 'this'): ?>
<?= '<p>yep</p>' ?>
<?php else: ?>
<?= '<p>nop</p>' ?>
<?php endif;
endforeach;
};
?>
edit/
Thank you
you cannot "break out of php" mid statement. wline defining an array for example you cannot close the php tags. The only time when you can "break out" of php is between opening and closing a loop or an if/else statement. This actualy does not break the statement as <?php foreach: ?> is a complete statement whereas <?php foreach{ ?> is not. Here some examples of what you can do:
<?php if($this!=$that): ?>
{something}
<?php endif ?>
<?php foreach($things as $thing): ?>
{something}
<?php endforeach ?>
<?php $while($this): ?>
{something}
<?php endwhile ?>
I think you get the message. you must have complete statements within php tags, without interruptions.
P.S. Also avoid using the shorthand <? instead of <?php at all cost, moving your project to a different hosting or an upgrade of your hosting might break your project as per default short tags are not activated. <?= ?> shorthand is safe as this is unaffected by the setting for newer php versions.
P.P.S Do not listen to the guy who wants php in one line, this will make your code hard to read and maintain. Stand strong and write beautiful code :)
UPDATE: (after the update on the question from #Jaume Mal)
I did not mean the examples in my answer as exclusive but as examples of statements that are complete vs statements that are incomplete. (I also forgot to mention closing php tags mid fuction, wich also work but I despise and woudl strongly advise against.) So for example <?php function foo(){ is a complete statement of starting a function but (as the other cases with loops etc..) it needs a closing statement, in this case }. This is true for if / else or foreach and so on:
<?php if($this){ ?>
some code
<?php } ?>
is a valid code, as the code pieces within the php tags are complete statements.

Which is the correct way to write an IF statement of these two? [duplicate]

Are there any differences between...
if ($value) {
}
...and...
if ($value):
endif;
?
They are the same but the second one is great if you have MVC in your code and don't want to have a lot of echos in your code. For example, in my .phtml files (Zend Framework) I will write something like this:
<?php if($this->value): ?>
Hello
<?php elseif($this->asd): ?>
Your name is: <?= $this->name ?>
<?php else: ?>
You don't have a name.
<?php endif; ?>
At our company, the preferred way for handling HTML is:
<? if($condition) { ?>
HTML content here
<? } else { ?>
Other HTML content here
<? } ?>
In the end, it really is a matter of choosing one and sticking with it.
They are indeed both the same, functionally.
But if the endif is getting too far from the correspondent if I think it's much better practice to give a referencing comment to it. Just so you can easily find where it was open. No matter what language it is:
if (my_horn_is_red or her_umbrella_is_yellow)
{
// ...
// let's pretend this is a lot of code in the middle
foreach (day in week) {
sing(a_different_song[day]);
}
// ...
} //if my_horn_is_red
That actually applies to any analogous "closing thing"! ;)
Also, in general, editors deal better with curly brackets, in the sense they can point you to where it was open. But even that doesn't make the descriptive comments any less valid.
Here's where you can find it in the official documentation: PHP: Alternative syntax for control structures
I think that it's particularly clearer when you're using a mix of ifs, fors and foreaches in view scripts:
<?php if ( $this->hasIterable ): ?>
<h2>Iterable</h2>
<ul>
<?php foreach ( $this->iterable as $key => $val ):?>
<?php for ( $i = 0; $i <= $val; $i++ ): ?>
<li><?php echo $key ?></li>
<?php endfor; ?>
<?php endforeach; ?>
</ul>
<?php elseif ( $this->hasScalar ): ?>
<h2>Scalar</h2>
<?php for ( $i = 0; $i <= $this->scalar; $i++ ): ?>
<p>Foo = Bar</p>
<?php endfor; ?>
<?php else: ?>
<h2>Other</h2>
<?php if ( $this->otherVal === true ): ?>
<p>Spam</p>
<?php else: ?>
<p>Eggs</p>
<?php endif; ?>
<?php endif; ?>
as opposed to:
<?php if ( $this->hasIterable ){ ?>
<h2>Iterable</h2>
<ul>
<?php foreach ( $this->iterable as $key => $val ){?>
<?php for ( $i = 0; $i <= $val; $i++ ){ ?>
<li><?php echo $key ?></li>
<?php } ?>
<?php } ?>
</ul>
<?php } elseif ( $this->hasScalar ){ ?>
<h2>Scalar</h2>
<?php for ( $i = 0; $i <= $this->scalar; $i++ ){ ?>
<p>Foo = Bar</p>
<?php } ?>
<?php } else { ?>
<h2>Other</h2>
<?php if ( $this->otherVal === true ){ ?>
<p>Spam</p>
<?php } else { ?>
<p>Eggs</p>
<?php } ?>
<?php } ?>
This is especially useful for long control statements where you might not be able to see the top declaration from the bottom brace.
I think that it really depends on your personal coding style.
If you're used to C++, Javascript, etc., you might feel more comfortable using the {} syntax.
If you're used to Visual Basic, you might want to use the if : endif; syntax.
I'm not sure one can definitively say one is easier to read than the other - it's personal preference. I usually do something like this:
<?php
if ($foo) { ?>
<p>Foo!</p><?php
} else { ?>
<p>Bar!</p><?php
} // if-else ($foo) ?>
Whether that's easier to read than:
<?php
if ($foo): ?>
<p>Foo!</p><?php
else: ?>
<p>Bar!</p><?php
endif; ?>
is a matter of opinion. I can see why some would feel the 2nd way is easier - but only if you haven't been programming in Javascript and C++ all your life. :)
I would use the first option if at all possible, regardless of the new option. The syntax is standard and everyone knows it. It's also backwards compatible.
Both are the same.
But:
If you want to use PHP as your templating language in your view files(the V of MVC) you can use this alternate syntax to distinguish between php code written to implement business-logic (Controller and Model parts of MVC) and gui-logic.
Of course it is not mandatory and you can use what ever syntax you like.
ZF uses that approach.
There is no technical difference between the two syntaxes. The alternative syntax is not new; it was supported at least as far back as PHP 4, and perhaps even earlier.
You might prefer the alternative form because it explicitly states which control structure is ending: endwhile, for example, can only terminate a while block, whereas if you encounter a brace, it could be closing anything.
You might prefer the traditional syntax, though, if you use an editor that has special support for braces in other C-like syntaxes. Vim, for example, supports several keystrokes for navigating to matching braces and to the starts and ends of brace-delimited blocks. The alternative syntax would break that editor feature.
In the end you just don't want to be looking for the following line and then having to guess where it started:
<?php } ?>
Technically and functionally they are the same.
It all depends, personally I prefer the traditional syntax with echos and plenty of indentations, since it's just so much easier to read.
<?php
if($something){
doThis();
}else{
echo '<h1>Title</h1>
<p>This is a paragraph</p>
<p>and another paragraph</p>';
}
?>
I agree alt syntax is cleaner with the different end clauses, but I really have a hard time dealing with them without help from text-editor highlighting, and I'm just not used to seeing "condensed" code like this:
<?php if( $this->isEnabledViewSwitcher() ): ?>
<p class="view-mode">
<?php $_modes = $this->getModes(); ?>
<?php if($_modes && count($_modes)>1): ?>
<label><?php echo $this->__('View as') ?>:</label>
<?php foreach ($this->getModes() as $_code=>$_label): ?>
<?php if($this->isModeActive($_code)): ?>
<strong title="<?php echo $_label ?>" class="<?php echo strtolower($_code); ?>"><?php echo $_label ?></strong>
<?php else: ?>
<?php echo $_label ?>
<?php endif; ?>
<?php endforeach; ?>
<?php endif; ?>
</p>
<?php endif; ?>
I used to use the curly braces but now a days I prefer to use this short-hand alternative syntax because of code readability and accessibility.
Personally I prefer making it in two seperate sections but within the same PHP like:
<?php
if (question1) { $variable_1 = somehtml; }
else { $variable_1 = someotherhtml; }
if (question2) {
$variable_2 = somehtml2;
}
else {
$variable_2 = someotherhtml2;
}
etc.
$output=<<<HERE
htmlhtmlhtml$variable1htmlhtmlhtml$varianble2htmletcetcetc
HERE;
echo $output;
?>
But maybe it is slower?
I think it's a matter of preference. I personally use:
if($something){
$execute_something;
}
I used to use curly brackets for "if, else" conditions. However, I found "if(xxx): endif;" is more semantic if the code is heavily wrapped and easier to read in any editors.
Of course, lots editors are capable of recognise and highlight chunks of code when curly brackets are selected. Some also do well on "if(xxx): endif" pair (eg, NetBeans)
Personally, I would recommend "if(xxx): endif", but for small condition check (eg, only one line of code), there are not much differences.
I feel that none of the preexisting answers fully identify the answer here, so I'm going to articulate my own perspective. Functionally, the two methods are the same. If the programer is familiar with other languages following C syntax, then they will likely feel more comfortable with the braces, or else if php is the first language that they're learning, they will feel more comfortable with the if endif syntax, since it seems closer to regular language.
If you're a really serious programmer and need to get things done fast, then I do believe that the curly brace syntax is superior because it saves time typing
if(/*condition*/){
/*body*/
}
compared to
if(/*condition*/):
/*body*/
endif;
This is especially true with other loops, say, a foreach where you would end up typing an extra 10 chars. With braces, you just need to type two characters, but for the keyword based syntax you have to type a whole extra keyword for every loop and conditional statement.

How to echo lots of HTML between an IF statement

At the moment my PHP code is like this:
<?php
some code
?>
lots of HTML
<?php
some more code
?>
I now want to include large chunks of HTML depending upon the values of certain PHP variables so like this:
<?php
if ($requiresSignature===true) {
echo "some HTML";
echo "some more HTML";
}
?>
Using echo is fine for a few lines of HTML but is there an easier way when I've got maybe 500 lines of HTML so I don't have to type echo in front of each line?
You can do it this way
<?php
if ($requiresSignature===true) {
?>
<b>some HTML</b>
<b>some more HTML</b>
<?php
}
?>
For this usage, the heredoc or nowdoc functionalities of php are the best options, in my humble opinion.
Heredoc
Heredoc is like echo "Foo bar"; but intended for a large chunk of text, spanning multiple lines.
Like this:
echo <<<FOO
<h1>Foo bar</h1>
<p>Lorem ipsum dolor sit tenet conseqteur...</p>
<i>Created by $name</i>
FOO;
This syntax is also available for setting variables, class properties, class constants and static variables (since php 5.3). The FOO part, you can set yourself. Just remember to close the Heredoc with the same ending on a line by itself (with absolutely no indentation), ended with a semicolon.
E.g.
$foo = <<<BAR
This is an example text.
Spanning multiple lines.
BAR;
Nowdoc
Think of Nowdoc as the ' equivalent of ". That is, no variable substitution is performed inside a Nowdoc statement, just like none is performed inside a 'single quoted string'.
The syntax is like this:
echo <<<'EXAMPLE'
This
is
a
test
EXAMPLE;
In conclusion I would do like this:
if ($requiresSignature===true) {
echo <<<HTML
Some html<br/>
And even more <b class="html">html</b>
HTML;
}
echo '
some html
some html
some html
';
I think that's what you're looking for
Try this
<?php
if (true){
?>
pure html code here
<?php
else{
?>
pure html code here
Try this
<?php
$Html = '';
if ($requiresSignature===true) {
$Html .="some HTML";
$Html .="some more HTML";
echo $Html;
}
?>
You can use heredoc syntax: See more detail in here
echo <<<"MYHTML"
html
lots of html
MYHTML;
this work for you
<?php if( var == true ): ?>
<p>Your HTML</p>
<?php endif; ?>
It's useful, try this code:
<?php
if ($requiresSignature===true) {
$var = "some HTML";
$var .= "some more HTML";
echo $var;
}
?>
**OR**
<?php if ($requiresSignature===true) { ?>
HTML CODE
<?php } ?>
Try this method,
<?php
$Html = "";
if ($requiresSignature===true) {
$Html .="some HTML";
$Html .="some more HTML";
echo $Html;
}
?>
You Don' Need to write echo to each line.
You Should use :
echo '
some html
some more html
some more html
';
PHP is a HTML embed language.You can try it every where of your php page.
<?php if ($requiresSignature===true): ?>
<p>"some more HTML"</p>
<?php endif;?>
...but is there an easier way when I've got maybe 500 lines of HTML so I don't have to type echo in front of each line? It turns out there is, so yes and here is how:
<?php
$myString = "PHP is so cool...";
if($requiredSignature === true):
?>
<div class='too-much-html-markup'>
All the RAW HTML MARKUP here would only be displayed if (and only if)
the condition above evaluates to true so i don't have to worry about
any kind of echoing. However, i can still go ahead and echo some
content here if i still choose like this: <?php echo $myString; ?>
And everything will still work out just fine.
</div>
<?php else: ?>
<div class='still-some-raw-html-in-else-clause'>
Again this is a raw Markup and will only be rendered if the IF
condition above evaluates to FALSE!
</div>
<?php endif; //<== NOW I END THE CONDITIONAL LOGIC WITH endif KEYWORD ?>
variables output in the plain HTML, consider doing so like this:
<?= $variable ?>
or
<?php echo $variable; ?>

$string is not showing in common.php

Let's say I've got 2 files. 1 is common which loads all the design and stuff and one is index.
What I want to do is set a $ in index like this:
<?
$SubId3 = 'test';
include "../../common.php";
?>
Then in common I want to have something like
<?=$SubId3; if (empty($SubId3)) { echo 'homepage'; } ?>
I cannot seem to get this working. Meaning if I set it up this way. The index will never show "test".
What am i doing wrong here?
I want to do this since only certain files will contain the string $SubId3, to test some things on certain pages and not others (by adding $SubId3 = 'test'; to that particular file)
Note that <?= is short-hand to output something (think of <?= as <?php echo) and not to execute any other sort of logic or code.
However, it is possible to use the ternary operator this way:
<?= empty($SubId3) ? 'homepage' : $SubId3; ?>
This is basically equivalent to this:
<?php
if (empty($SubId3)) {
echo 'homepage';
}
else {
echo $SubId3;
}
?>
So the <?= short-hand should only be used to pass one simple variable or a ternary expression to it; everything else should use the common <?php tag.
Here's a test case for Alex (in the comments) because I can run the above code just fine with PHP 5.4.12, but he seems not to be able to.
common.php
<?= empty($SubId3) ? 'homepage' : $SubId3; ?>
index.php (visit this file then)
<?php
$SubId3 = 'test'; // <-- Comment this out for the "homepage" output
include 'common.php';
i think this
<?=$SubId3; if (empty($SubId3)) { echo 'homepage'; } ?>
should be
<?php $SubId3; if (empty($SubId3)) { echo 'homepage'; } ?>
<?=?> is short for <?php echo?>
This wont work:
<?=$SubId3; if (empty($SubId3)) { echo 'homepage'; } ?>
If you want to print some stuff, you have to use only the variable, in one block and the IF on another.
<?=$SubId3?>
And:
<?php if(empty($SubId3)) { echo 'homepage'; } ?>
Hope this helps...
Try
<?php
/* echo $SubId3; */
if (empty($SubId3)) {
echo 'homepage';
} else {
echo $SubId3;
}
?>
Consider using different style of coding.
In PHP you have generally three variants:
PHP code only
HTML files with just some echoes
Intermixed PHP and HTML
In first you use echo to output every single bit of the HTML.
Second means you include a PHP script at the top of your HTML file and call appropriate functions / insert text into the template. Just so you can edit your HTML separately from your PHP.
Third makes for sometimes unreadable and complex code, but is fast to write.
<?php if($something) {
while($otherthing) { ?>
<B>text=<?=$index ?></B>
<?php }} ?>
Just a food for thought.
I found the answer guys, thanks for all the help.
I needed to set it in the PrintHeader like this:
<?
include "../../common.php";
printHeader('BlogNr1', 'BlogNr2', 'BlogNr3');
?>
And the index had to look like this:
<?
include "../../common.php";
printHeader('BlogNr1', 'BlogNr2', 'BlogNr3');
?>
Somebody on skype helped me. thanks anyways guys!

How can I echo HTML in PHP?

I want to conditionally output HTML to generate a page, so what's the easiest way to echo multiline snippets of HTML in PHP 4+? Would I need to use a template framework like Smarty?
echo '<html>', "\n"; // I'm sure there's a better way!
echo '<head>', "\n";
echo '</head>', "\n";
echo '<body>', "\n";
echo '</body>', "\n";
echo '</html>', "\n";
There are a few ways to echo HTML in PHP.
1. In between PHP tags
<?php if(condition){ ?>
<!-- HTML here -->
<?php } ?>
2. In an echo
if(condition){
echo "HTML here";
}
With echos, if you wish to use double quotes in your HTML you must use single quote echos like so:
echo '<input type="text">';
Or you can escape them like so:
echo "<input type=\"text\">";
3. Heredocs
4. Nowdocs (as of PHP 5.3.0)
Template engines are used for using PHP in documents that contain mostly HTML. In fact, PHP's original purpose was to be a templating language. That's why with PHP you can use things like short tags to echo variables (e.g. <?=$someVariable?>).
There are other template engines (such as Smarty, Twig, etc.) that make the syntax even more concise (e.g. {{someVariable}}).
The primary benefit of using a template engine is keeping the design (presentation logic) separate from the coding (business logic). It also makes the code cleaner and easier to maintain in the long run.
If you have any more questions feel free to leave a comment.
Further reading is available on these things in the PHP documentation.
NOTE: PHP short tags <? and ?> are discouraged because they are only available if enabled with short_open_tag php.ini configuration file directive, or if PHP was configured with the --enable-short-tags option. They are available, regardless of settings from 5.4 onwards.
Try it like this (heredoc syntax):
$variable = <<<XYZ
<html>
<body>
</body>
</html>
XYZ;
echo $variable;
You could use the alternative syntax alternative syntax for control structures and break out of PHP:
<?php if ($something): ?>
<some /> <tags /> <etc />
<?=$shortButControversialWayOfPrintingAVariable ?>
<?php /* A comment not visible in the HTML, but it is a bit of a pain to write */ ?>
<?php else: ?>
<!-- else -->
<?php endif; ?>
Basically you can put HTML anywhere outside of PHP tags. It's also very beneficial to do all your necessary data processing before displaying any data, in order to separate logic and presentation.
The data display itself could be at the bottom of the same PHP file or you could include a separate PHP file consisting of mostly HTML.
I prefer this compact style:
<?php
/* do your processing here */
?>
<html>
<head>
<title><?=$title?></title>
</head>
<body>
<?php foreach ( $something as $item ) : ?>
<p><?=$item?></p>
<?php endforeach; ?>
</body>
</html>
Note: you may need to use <?php echo $var; ?> instead of <?=$var?> depending on your PHP setup.
I am partial to this style:
<html>
<head>
<% if (X)
{
%> <title>Definitely X</title>
<% }
else
{
%> <title>Totally not X</title>
<% }
%> </head>
</html>
I do use ASP-style tags, yes. The blending of PHP and HTML looks super-readable to my eyes. The trick is in getting the <% and %> markers just right.
Another approach is put the HTML in a separate file and mark the area to change with a placeholder [[content]] in this case. (You can also use sprintf instead of the str_replace.)
$page = 'Hello, World!';
$content = file_get_contents('html/welcome.html');
$pagecontent = str_replace('[[content]]', $content, $page);
echo($pagecontent);
Alternatively, you can just output all the PHP stuff to the screen captured in a buffer, write the HTML, and put the PHP output back into the page.
It might seem strange to write the PHP out, catch it, and then write it again, but it does mean that you can do all kinds of formatting stuff (heredoc, etc.), and test it outputs correctly without the hassle of the page template getting in the way. (The Joomla CMS does it this way, BTW.)
I.e.:
<?php
ob_start();
echo('Hello, World!');
$php_output = ob_get_contents();
ob_end_clean();
?>
<h1>My Template page says</h1>
<?php
echo($php_output);
?>
<hr>
Template footer
$enter_string = '<textarea style="color:#FF0000;" name="message">EXAMPLE</textarea>';
echo('Echo as HTML' . htmlspecialchars((string)$enter_string));
Simply use the print function to echo text in the PHP file as follows:
<?php
print('
<div class="wrap">
<span class="textClass">TESTING</span>
</div>
')
?>
In addition to Chris B's answer, if you need to use echo anyway, still want to keep it simple and structured and don't want to spam the code with <?php stuff; ?>'s, you can use the syntax below.
For example you want to display the images of a gallery:
foreach($images as $image)
{
echo
'<li>',
'<a href="', site_url(), 'images/', $image['name'], '">',
'<img ',
'class="image" ',
'title="', $image['title'], '" ',
'src="', site_url(), 'images/thumbs/', $image['filename'], '" ',
'alt="', $image['description'], '"',
'>',
'</a>',
'</li>';
}
Echo takes multiple parameters so with good indenting it looks pretty good. Also using echo with parameters is more effective than concatenating.
echo '
<html>
<body>
</body>
</html>
';
or
echo "<html>\n<body>\n</body>\n</html>\n";
Try this:
<?php
echo <<<HTML
Your HTML tags here
HTML;
?>
This is how I do it:
<?php if($contition == true){ ?>
<input type="text" value="<?php echo $value_stored_in_php_variable; ?>" />
<?php }else{ ?>
<p>No input here </p>
<?php } ?>
Don't echo out HTML.
If you want to use
<?php echo "<h1> $title; </h1>"; ?>
you should be doing this:
<h1><?= $title;?></h1>

Categories