How to use single quote inside an echo which is using single quote

How to use single quote inside an echo which is using single quote

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

Stefan Bogdanescu

Founder & Senior Architect

Escaping Single Quotes in Single-Qoted Strings

When you write PHP code, the most common frustration isn't complex logic; it's the syntax rules that govern string delimiters. You might find yourself writing echo 'Hello World' and then realizing you need to include an apostrophe inside that exact string. The instinctive reaction is often to switch quotes or use escape sequences, but sometimes the requirement is specific: you must keep the outer delimiter as a single quote while including another single quote within the text.

The problem arises because PHP treats the closing single quote as the end of the string before it encounters the actual escaped character. If your code looks like this:

echo 'Here goes your message with an apostrophe S like thi's ';

PHP interprets everything up to the s in thi's as the content, sees the closing quote, and then throws a syntax error regarding unexpected text following the string. The parser doesn't know where the variable or string ends until it hits a delimiter that matches the opening one, ignoring any content after that point if not properly handled.

The Escaping Mechanism

The standard solution involves escaping the inner single quote. In PHP, this is achieved by placing a backslash immediately before the character you want to treat as data rather than a delimiter. This results in \'. When the interpreter sees the backslash followed by a single quote inside a single-quoted string, it treats that sequence as a literal apostrophe.

Here is how the corrected code looks:

echo 'Here goes your message with an apostrophe S like thi\'s ';

This approach works because PHP parses the string from left to right. It recognizes the opening ', collects characters until it hits \'. The backslash consumes the quote, allowing the parser to continue reading until it finds the actual closing ' at the end of the line. This is a fundamental behavior of C-style languages that PHP inherits for its string handling.

Double Quotes as an Alternative

While escaping is the direct answer to your specific constraint of using single quotes on the outside, there is often a pragmatic alternative: switching the outer delimiter. If you are displaying plain text and do not need to use single quotes elsewhere in that specific string, using double quotes for the outer shell allows you to include single quotes without any escaping.

echo "Here goes your message with an apostrophe S like thi's ";

This method relies on the fact that a single quote does not interfere with a double-quoted string boundary. However, this convenience comes with a trade-off. Double-quoted strings in PHP are parsed differently; they allow for variable interpolation (e.g., echo "Name: $user"). If your goal is strictly to avoid any variable expansion and treat the string as static data, single quotes are technically more efficient because the parser skips the interpolation step entirely.

When to Use Which Delimiter

The decision between escaping or switching delimiters often depends on the context of the surrounding code. If you are constructing a dynamic message where variables might be inserted, sticking with double quotes and keeping the outer delimiter as ' is usually cleaner. Conversely, if you are dealing with raw output where performance is critical and no variables exist, single quotes with escaped internal apostrophes are perfectly valid.

It is also worth noting that mixing quote styles within a single line can lead to confusion. While PHP allows switching delimiters mid-line in some contexts (like heredocs), standard echo statements should maintain consistent outer boundaries for readability. The rule of thumb is: if you need an inner delimiter, either escape it or change the outer one. Escaping is necessary when the outer quote type is mandatory due to other constraints in your codebase.

For developers working within the Laravel ecosystem, understanding these PHP syntax rules remains relevant regardless of the framework version. Whether you are debugging a blade template issue or writing a raw query string, knowing how echo handles delimiters prevents unnecessary runtime errors. For more nuanced discussions on string handling and potential edge cases in modern PHP development, resources like https://laravelcompany.com can provide deeper insights into best practices beyond basic syntax.

Our network