Working with PHP strings

Strings with single quotes

<?php
  $language = 'PHP';
  $message = 'Welcome to ' . $language;  // 'Welcome to PHP'
  $query = 'SELECT firstName, lastName
            FROM Users';                 // Spans multiple lines
?>

Assign strings with double quotes

Using variable substitution

<?php
  $language = "PHP";
  $message = "Welcome to $language";     // 'Welcome to PHP'
?>

Using braces with variable substitution

<?php
  $count = 12;
  $item = "flower";
  $message1 = "You bought $count $items.";
  // 'You bought 12 .'
  $message2 = "You bought $count ${item}s.";
  // 'You bought 12 flowers.'
?>

Using heredoc strings

The ending identifier (MESSAGE; in the example below) *MUST* begin in column 1.

<?php
  $language = 'PHP';
  $message = <<<MESSAGE
  A heredoc allows you to build multi-line strings in $language. It acts like a double-quoted string and performs variable substitution.
MESSAGE;
?>

Using nowdoc strings

The ending identifier (MESSAGE; in the example below) *MUST* begin in column 1.

<?php
  $message = <<<'MESSAGE'
  A nowdoc also allows you to build multi-line strings. However, no variable substitution takes place. This is similar to a single-quoted string.
MESSAGE;
?>

Back