Confused about echoing html with PHP

Hi,

Believe or not I’m confused with something as simple as echoing with php. The reason I’m confused is because I try echoing some html differently and to my surprise it worked.

I tried…

<?php
$car = ‘cobalt’;
echo"<h1>“.$car.”</h1>";
?>

I also tried …

echo"<h1>$car</h1>";

it worked… I tried

echo(“<h1>$car</h1>”);

and it also worked, I got the same result with all three, why?

At this point I’m very confused, can someone be so kind and explain the different echoing options?

Thanks a lot!

Wow! thanks a lot for your comments, now, I have also seen some cases where an HTML tag is opened with an echo and closes with another echo something like this…

echo‘<div>’;
//echo some html tags here
echo’</div>’;

Is this a valid way to echo HTML?
How does PHP know to close the tag after a few lines of code?

With the method above do I need to echo any HTML inside print<<<HERE HERE; or no? In other word would something like this work… without echoing every tag?

print<<<HERE
<p> some text here
<div>Some content here</div>
<div>Some content here</div>
HERE;

Thanks a lot!

you can even do


$whatsup = "Another Echo Method"; 
<?= $whatsup; ?>

another method


print<<<HERE
anything in html here 

lots of html and you can mix php code without worrying about opening and closing php code. 


HERE;

Thanks a lot for your comments.

First of all thanks a lot!

So, there is no good or wrong?

I tend to use something like the following:


<html>
  <head>
    <title>Title</title>
  </head>
  <body>
    <?php foreach($posts as $post): ?>
      <div class="post">
        <h1>
          <?php echo $post->title; ?>
        </h1>
        <div>
          <?php echo $post->summary; ?>
        </div>
        <a href="<?php echo $post->link; ?>">Read more</a>
      </div>
    <?php endforeach; ?>
  </body>
</html>

Yes it’s valid. The important thing is that in the end the resulting page is valide HTML, and you can check that in your browser :slight_smile:

How does PHP know to close the tag after a few lines of code?

It doesn’t “know”, and it doesn’t close anything. You tell it to display the closing tag, but PHP doesn’t know it’s a closing tag. It’s just text to be displayed.

With the method above do I need to echo any HTML inside print<<<HERE HERE; or no? In other word would something like this work… without echoing every tag?

print<<<HERE
<p> some text here
<div>Some content here</div>
<div>Some content here</div>
HERE; 

Thanks a lot!

Did you try? :slight_smile:

Good read - thanks for sharing :smiley:

There’s also a third option, using commas instead of dots. Supposedly faster :slight_smile:

<?php echo '<h1>' , $cat , '</h1>'; ?>

Note that ‘<h1>’, $cat and ‘</h1>’ are now just different parameters to the echo statement, the comma is not an alternative way of concatenating strings.

I’ll take a stab at this, but I’m sure a pro will chime in as well.

<?php echo (‘Hello’); ?> is the same as <?php echo ‘Hello’; ?>

The parentheses are optional so this should address why example #1 & #2 are the same as example #3.

In example #1, you are using concatenation to combine two HTML elements with a PHP string. Typically, this is done when using SINGLE quotes instead of DOUBLE quotes.

<?php echo ‘<h1>’ . $car . ‘</h1>’; ?>

Remember that PHP will read text literally within single quotes (ie. will not display a variable’s value when declared; just the actual variable name itself), but when encased within double quotes, PHP will echo the variable’s value.

That being said, your first example is technically making PHP “work harder” because those double quotes are telling it to, in simple terms, to search through the text and find any variables then display their value. Since you only have <h1> and </h1> there is no need to use double quotes.

With this in mind, you can write your code one of two ways (each properly utilizing quotes in PHP):

<?php echo “<h1>$cat</h1>”; ?>

-OR-

<?php echo ‘<h1>’ . $cat . ‘</h1>’; ?>

Hope I helped :slight_smile:

Very good examples! Thank you all very much.

take exactly that and put it in your new document. you can add php code and echo statements or whatever just regular html code without opening and closing tags… it was a trick i learned a few years ago. itll confuse the hell out of designers though lol!


<? 
print<<<HERE
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>

this is here 

</body>
</html>
HERE;
?>




The problem of commas is when you refactor your code and put those strings you’re echoing into some variable : you have to replace them instead of some easy copy-pasta. Just for less than 10% perfs on something which is not your bottleneck and may have different results depending on your php version : better use the easier to maintain version.

well echoing with brackets is very uncommon, and makes your code look a bit weird, so I’d avoid that even though it works.
The rest is stylistic.
For simple variables I like the way a single statement looks without concatenation:


echo "<h1>$heading</h1>";        //looks better IMO
echo "<h1>", $heading . "</h1>"; //looks worse IMO

If you’re doing function calls then concat or commas are okay.


echo '<h1>', $page->get_heading(), '</h1>';

echo '<h2>', htmlspecialchars($heading), '</h2>';

PHP has a very open semantics, you can do the same thing in very different ways and have huge liberties. It does suggest you to do crappy coding, and many does, but it also helps you develop very fast, and no need to debug much.

The way I do it is usually the first example, it is what makes most sense for my style of coding.