PHP Echo a large block of text

PHP Echo a large block of text

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Echoing Large Blocks of Text in PHP: String Concatenation vs. Raw Output

A common stumbling block for developers new to PHP is understanding how echo handles multi-line strings. The confusion often stems from mixing up the rules of string literals with the behavior of the echo language construct. In the scenario you presented, the issue isn't that echo can only handle short strings; it is that your code violates two fundamental syntax rules: single quotes cannot span multiple lines, and an unclosed string literal will cause a fatal error before the script finishes executing.

When you write a string using single quotes (') or double quotes (") in PHP, the parser expects a closing delimiter on the same line where the string opens. If you hit the Enter key inside a single-quoted string without escaping the newline character or closing the quote, PHP interprets this as a syntax error. The moment it encounters the first actual newline character inside the single quotes, it doesn't automatically wrap to the next line; instead, it throws a parse error because it hasn't seen the closing ' yet.

To fix the specific error in your snippet, you must ensure every string literal is properly terminated before starting the next one, or use a different mechanism entirely. The most straightforward approach for outputting large blocks of CSS or JavaScript is to treat the entire block as a single string variable using concatenation. This allows you to break the code across multiple lines for readability without breaking the syntax.

Here is how you can restructure that block:

if (is_single()) {
    $styles = <<<EOD
<link type="text/css" rel="stylesheet" href="http://jotform.com/css/styles/form.css"/>
<style type="text/css"> 
.form-label{
    width:150px !important;
}
.form-line{
    padding:10px;
}
</style> 

<script src="http://jotform.com/js/jotform.js?v3" type="text/javascript"></script>
EOD;

    echo $styles;
}

The <<<EOD syntax is known as a "Heredoc". This is the preferred method for embedding large blocks of text, such as HTML or CSS, directly within PHP files. It behaves similarly to double-quoted strings (variables inside are parsed) but offers superior formatting capabilities. You define a label (EOD in this case), and everything between that label and the closing <<<EOD; is captured as a string. The crucial detail is that the closing label must be on its own line, indented exactly like the opening tag.

Alternatively, if you prefer not to use Heredocs, you can simply concatenate strings or use double quotes with explicit newline escapes:

if (is_single()) {
    $output = '<link href="..." />' . "\n" . 
               '<style>' . "\n" . 'body{margin:0;}' . "\n" . '</style>';
    
    echo $output;
}

While concatenation works, it quickly becomes unreadable and error-prone as the block grows. Heredocs keep the code clean by allowing you to maintain the original indentation of your CSS or JS while keeping the PHP syntax valid.

One critical pitfall to watch for when using Heredocs or double-quoted strings is variable interpolation. If you use <<<EOD without a semicolon after the label, variables inside the block (like $user_name) will be parsed and replaced with their values. If you want to output raw code where you don't want PHP variables processed, you must add a semicolon immediately after the opening label:

$raw_css = <<<EOD;
<style>
    .class { $user_variable } // This will NOT replace $user_variable if it's not defined or if you want raw text
</style>
EOD;

If you use a semicolon, the content inside is treated as a literal string, preventing unwanted variable interpolation. This distinction is vital when echoing dynamic content versus static assets like third-party libraries.

Another consideration involves performance and file structure. For very large blocks of code that rarely change, such as vendor libraries or standard asset links, it is often better to externalize them into separate .php files included via require_once, rather than embedding them directly in your logic. This keeps your main templates cleaner and makes caching easier. However, if you are dynamically generating a page where the assets need to be conditional (like checking is_single()), outputting them directly via an echo or Heredoc within that conditional block is the correct architectural choice.

Our network