techmore.in

PHP - Heredoc & Nowdoc

Heredoc and nowdoc are syntax features in PHP that allow you to define multi-line strings easily without worrying about escaping quotes or special characters. They are particularly useful when you need to define large blocks of text, such as HTML, SQL queries, or even code snippets. Here’s an explanation of both heredoc and nowdoc syntax in PHP:

Heredoc Syntax

Heredoc syntax starts with <<< followed by an identifier (often in uppercase) and ends with the same identifier followed by a semicolon. The closing identifier must appear at the beginning of a line with no other characters, except possibly whitespace. Heredoc strings behave like double-quoted strings, meaning they support variable interpolation.

Syntax:

php
$str = <<<EOD This is a heredoc string. It can span multiple lines. Variables like $name are interpolated. EOD;
  • EOD is the identifier that marks the start and end of the heredoc string. It can be any valid identifier name.
  • The closing identifier (EOD in the example) must be followed by a semicolon (;) on its own line.

Example:

php
<?php $name = "John"; $str = <<<EOD Hello, $name! This is a heredoc string. It can span multiple lines. EOD; echo $str; ?>

Nowdoc Syntax

Nowdoc syntax is similar to heredoc, but it behaves like single-quoted strings. This means no variable interpolation or escape sequences are processed within nowdoc strings. Nowdoc strings are useful when you want to output text exactly as it appears.

Syntax:

php
$str = <<<'EOD' This is a nowdoc string. It behaves like single-quoted strings. Variables like $name are not interpolated. EOD;
  • EOD is the identifier, similar to heredoc syntax.
  • The closing identifier (EOD in the example) must be followed by a semicolon (;) on its own line.

Example:

php
<?php $name = "John"; $str = <<<'EOD' Hello, $name! This is a nowdoc string. It behaves like single-quoted strings. EOD; echo $str; ?>

Key Differences Between Heredoc and Nowdoc

  1. Variable Interpolation:

    • Heredoc strings allow variable interpolation (like double-quoted strings).
    • Nowdoc strings treat variables as literals (like single-quoted strings).
  2. Escape Sequences:

    • Heredoc strings process escape sequences (\n, \t, etc.) and special characters ($, \, etc.).
    • Nowdoc strings do not process any escape sequences or special characters. They are treated literally.

When to Use Heredoc vs Nowdoc

  • Heredoc is useful when you need to include variables or process escape sequences within your multi-line string.
  • Nowdoc is useful when you want to ensure that the text within the string is treated exactly as it appears, without any interpolation or processing.

Summary:

Heredoc and nowdoc syntax in PHP provide convenient ways to define multi-line strings. Heredoc strings support variable interpolation and escape sequences, while nowdoc strings do not process variables or escape sequences, treating their content as raw text. Understanding when and how to use these syntaxes can simplify the handling of large blocks of text within PHP applications.