Solution 1 :

As an alternative, you may use Output Control Functions:

Your code becomes:

<?php
ob_start();
?>

<div class='item'>
  <img src='chicago.jpg' alt='Chicago'>
  <div class='carousel-caption'>
    <h3>Chicago</h3>
    <p>Thank you, Chicago!</p>
  </div>
</div>

<?php 
$html_block1 = ob_get_clean();

This way also allows you to move your HTML templates to a view file:

script.php

<?php
ob_start();
require VIEW_FOLDER . '/my_view.php';
$html_block1 = ob_get_clean();

my_view.php

<div class='item'>
  <img src='chicago.jpg' alt='Chicago'>
  <div class='carousel-caption'>
    <h3>Chicago</h3>
    <p>Thank you, Chicago!</p>
  </div>
</div>

If you’re going to do stuff like this, you should however consider looking into template engines, such as Twig.

Problem :

I am trying to assign some html to a variable in php in such a way in an effort to modularise my html.

$html_block1 = "<div class='item'>
      <img src='chicago.jpg' alt='Chicago'>
      <div class='carousel-caption'>
        <h3>Chicago</h3>
        <p>Thank you, Chicago!</p>
      </div>
    </div>" 

Html specific Markup however is lost since every text editor will consider this as a simple string instead of the html it indeed it. This makes later editing very impractical. Is there is a way to store this html in a php file without loosing the markup in a way that can easily be used later by other files? Thank you.

Comments

Comment posted by heredoc syntax

Dependent on your editor,

By