How to echo script in php

How to echo script in php

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

Stefan Bogdanescu

Founder & Senior Architect

Escaping PHP Strings to Output JavaScript Correctly

When embedding dynamic content into a WordPress site using plugins like the Shortcode Execute plugin, you often need to bridge the gap between server-side logic and client-side behavior. A common scenario involves echoing a <script> block directly from PHP to handle tasks like spam protection for email addresses. However, mixing PHP syntax with JavaScript inside a single echoed string introduces immediate conflicts regarding quote delimiters.

Your core issue stems from using single quotes in both the PHP string definition and the inner JavaScript code. When you write echo '...';, the PHP parser treats everything inside those outer quotes as literal text until it hits an unescaped delimiter or encounters a variable. If your JavaScript logic contains apostrophes (single quotes), the PHP parser prematurely terminates the string at the first occurrence, throwing a syntax error before the code ever reaches the browser.

To resolve this, you must distinguish between the string delimiter used in PHP and the characters contained within that string. The most robust solution is to switch the outer PHP string delimiter to double quotes while keeping the inner JavaScript code using single quotes. This creates a clear boundary for the PHP parser.

Consider the following corrected implementation:

<?php
echo <<<EOT
<script type="text/javascript">
    // spam protected email
    var emailE = ("enquiries@" + "example.co.uk");
    document.write('<a title="E-mail Example" href="mailto:' + emailE + '">' + emailE + '</a>');
</script>
<noscript>
    <span class="spam-protected">Email address protected by JavaScript. Please enable JavaScript.</span>
</noscript>
EOT;
?>

In this example, we utilize a Here Document (<<<EOT). This syntax allows you to write multi-line strings without worrying about escaping newlines or closing the string prematurely with quote characters inside the block. The only requirement is that the delimiter EOT does not appear anywhere within the script content. This approach keeps the code readable and eliminates the need for excessive backslash escaping.

If you prefer standard double quotes instead of a Here Document, you must escape the inner double quotes used in HTML attributes or JavaScript strings. For instance, changing the document.write line to use escaped quotes:

echo "<script type=\"text/javascript\">
    var emailE = ('enquiries@' + 'example.co.uk');
    document.write(\"<a title='E-mail Example' href='mailto:" + emailE + "'>" + emailE + "</a>\");
</script>";

Here, the backslashes (\) tell PHP to treat the following double quote as a literal character within the string rather than closing it. While functional, this method becomes difficult to maintain as the code grows in complexity. The Here Document approach remains superior for larger blocks of HTML or JavaScript because it preserves formatting and reduces cognitive load regarding escape sequences.

Another consideration involves variable interpolation. If you need to inject PHP variables into the generated script, double quotes are required for string interpolation. However, if your variables contain characters that conflict with the outer delimiter, you might encounter issues similar to the quote collision problem described above. In such cases, using single quotes for the PHP string and escaping inner single quotes, or vice versa depending on the content, is necessary.

Finally, ensure that any output buffering settings in WordPress do not interfere with the direct echo. Since this is intended for a shortcode execution environment, the script block should be printed directly to the current buffer. By carefully managing your quote delimiters and choosing the appropriate string syntax, you can safely generate complex JavaScript logic from PHP without triggering parse errors.

Our network