Strings in PHP

Single vs double quotes

Single quotes print text exactly, double quotes interpolate variables and read escape sequences like newline:

<?php echo "First\nSecond\n"; // two lines echo 'No \n here', PHP_EOL; // prints \n as text

php quotes.php

First
Second
No \n here

The first string holds a real line break, because a double quoted string reads \n; the second keeps the backslash and the letter n, because a single quoted string reads neither escapes nor variables. Every string in PHP ends where its quote ends, and PHP_EOL is the portable way to end the line.

Concatenation with dot

The dot glues strings together. Surround it with spaces so the line stays readable:

<?php $first = "Ada"; $last = "Lovelace"; $full = $first . " " . $last; echo $full; // Ada Lovelace

php concat.php

Ada Lovelace

The space between the names is a string of its own: without it the result would be AdaLovelace. The dot never adds a separator by itself, so every space the reader sees has to be in the code.

Heredoc and nowdoc

Long texts read better as heredoc: an opening marker, plain lines, and the same marker at the start of the closing line. Variables still interpolate inside:

<?php $name = "Ada"; $text = <<<TEXT Hello, $name! Welcome back. TEXT; echo $text;

php heredoc.php

Hello, Ada!
Welcome back.

Nowdoc uses the same shape with a quoted marker and never interpolates, which fits samples with dollar signs:

<?php $sample = <<<'TEXT' Price: $5, no variables here. TEXT; echo $sample;

php nowdoc.php

Price: $5, no variables here.

Length and access by offset

strlen() counts bytes, and square brackets read one byte by its offset starting from zero:

<?php $word = "PHP"; echo strlen($word), PHP_EOL; // 3 echo $word[0]; // P echo $word[2]; // P

php length.php

3
PP

The two letters print together, because echo does not end a line between them. Offset 0 is P, offset 1 is H and offset 2 is the second P; an offset past the end prints a warning and an empty string.

For alphabets beyond ASCII use mb_strlen(), because strlen counts bytes and not characters.

Exercises

  1. Print Ada and Lovelace on one line with a single echo, using interpolation instead of the dot.
  2. Remove the trailing \n from the first string in quotes.php, run the file, and explain the output.
  3. Put a variable and a dollar price in one nowdoc and check that the variable is printed as written.
  4. Print the middle letter of PHP, then the letter at offset 3. What does the second call print, and what does PHP warn about?

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. Which string form reads \n as a line break?

    Answer

    A double quoted string

    A double quoted string (and a heredoc) reads escape sequences and interpolates; a single quoted string and a nowdoc print the backslash as text.

  2. What does the marker <<<'TEXT' open?

    Answer

    A nowdoc, which prints everything as written

    The quoted marker makes it a nowdoc: the lines between the markers are taken literally, dollar signs included.

  3. What does strlen() count?

    Answer

    Bytes

    strlen counts bytes, so a UTF-8 letter of two bytes adds two to its result.

  4. Which function counts characters instead of bytes?

    Answer

    mb_strlen()

    mb_strlen reads the encoding and counts characters, which is what a reader sees.

  5. What does this program print? <?php echo "First\nSecond";

    Answer

    First / Second

    The escape \n inside a double quoted string becomes a real line break, so the two words print on two lines.

  6. What does this program print? <?php echo 'No \n here';

    Answer

    No \n here

    A single quoted string reads no escapes, so the backslash and the letter n print as they are written.

  7. What does this program print? <?php $word = "PHP"; echo $word[1];

    Answer

    H

    The offset counts from zero, so offset 1 is the second letter of the string.

  8. What does this program print? <?php $first = "Ada"; $last = "Lovelace"; echo $first . " " . $last;

    Answer

    Ada Lovelace

    The middle string holds the space, and the dot joins the three parts in order.

  9. What does this program print? <?php $name = "Ada"; $text = <<<TEXT Hello, $name! Welcome back. TEXT; echo $text;

    Answer

    Hello, Ada! / Welcome back.

    A heredoc interpolates the variable and keeps the line breaks of its lines, and the closing marker is not part of the text.

  10. What does this program print? <?php $sample = <<<'TEXT' Price: $5, no variables here. TEXT; echo $sample;

    Answer

    Price: $5, no variables here.

    A nowdoc prints its lines literally, so the dollar sign and the digit stay in the output.

The code on this page was run with PHP 8.5.13 (php -v) in Alpine Linux, with the commands shown above.

Article author: Arthur Isaev