TExcelTAD
Home › PHP › PHP Strings

PHP Strings

A string is a sequence of characters such as "Hello", 'order #42' or an entire HTML page. Because PHP is mostly used to build text that a browser displays, strings are the data type you will work with more than any other.

Single quotes vs double quotes

PHP has two everyday ways to write a string literal, and they behave differently:

Literal Variables expanded? Escape sequences Example
'single' No Only \' and \\ 'Total: $sum\n' stays exactly as typed
"double" Yes \n, \t, \$, \", \\, \u{1F418} … "Total: $sum\n" inserts the value and a newline
php · try it yourself
<?php
$fruit = "mango";

echo 'I like $fruit\n', PHP_EOL;
echo "I like $fruit\n";
echo "Tab\tseparated\tcolumns\n";
echo "A literal dollar sign: \$fruit\n";
echo 'It\'s a single-quoted string', "\n";
I like $fruit\n
I like mango
Tab	separated	columns
A literal dollar sign: $fruit
It's a single-quoted string

Tip: Use single quotes when the text is fixed and contains no variables — what you see is exactly what you get. Switch to double quotes when you want interpolation or \n.

Interpolation: putting variables inside strings

Inside double quotes PHP replaces simple variables with their values. For anything more complex — array keys with quotes, object properties chained together, or a variable directly followed by letters — wrap the expression in curly braces:

php · try it yourself
<?php
$item = "pencil";
$count = 3;
$price = ["pencil" => 0.75];
$user = new stdClass();
$user->name = "Mai";

echo "You bought $count $item.\n";
echo "Plural: {$item}s\n";              // without braces PHP looks for $items
echo "Unit price: {$price['pencil']}\n";
echo "Customer: {$user->name}\n";
You bought 3 pencil.
Plural: pencils
Unit price: 0.75
Customer: Mai

Joining strings with the dot operator

PHP uses . (not +) to concatenate strings, and .= to append to an existing variable. The + operator is reserved for numbers.

php · try it yourself
<?php
$first = "Ada";
$last = "Lovelace";

$full = $first . " " . $last;
echo $full . "\n";

$log = "start";
$log .= " -> load";
$log .= " -> done";
echo $log . "\n";

echo "Length of full name: " . strlen($full) . "\n";
Ada Lovelace
start -> load -> done
Length of full name: 12

Heredoc and nowdoc

For multi-line text, PHP offers two block syntaxes. Heredoc (<<<LABEL) behaves like a double-quoted string, nowdoc (<<<'LABEL') like a single-quoted one. Since PHP 7.3 the closing label may be indented, and that indentation is removed from every line.

php · try it yourself
<?php
$name = "Linh";
$total = 18.5;

$receipt = <<<TEXT
    Customer: $name
    Total:    {$total} USD
    Thank you!
    TEXT;
echo $receipt, "\n";

$template = <<<'RAW'
    Variables like $name are NOT replaced here.
    RAW;
echo $template, "\n";
Customer: Linh
Total:    18.5 USD
Thank you!
Variables like $name are NOT replaced here.

Accessing single characters

A string can be indexed like an array, starting at 0. Negative offsets count from the end (-1 is the last character).

php · try it yourself
<?php
$word = "elephant";

echo $word[0], "\n";
echo $word[3], "\n";
echo $word[-1], "\n";
echo strlen($word), " characters\n";
e
p
t
8 characters

Note: strlen() counts bytes, not characters. For text with accents or emoji (UTF-8), use mb_strlen() — for example strlen("café") is 5 but mb_strlen("café") is 4.

Summary

Task Syntax
Literal text 'text'
Text with variables / escapes "Hello $name\n"
Complex expression in a string "{$arr['key']}", "{$obj->prop}"
Concatenate $a . $b
Append $a .= $b
Multi-line with variables heredoc <<<EOT
Multi-line raw nowdoc <<<'EOT'

Test yourself

What does echo 'Hi $name'; print when $name = "Bo"?

  1. Hi Bo
  2. Hi $name
  3. Hi
  4. An error

Which operator joins two strings in PHP?

  1. +
  2. &
  3. .
  4. ,

Which heredoc-style syntax does NOT expand variables?

  1. <<<EOT
  2. <<<"EOT"
  3. <<<'EOT' (nowdoc)
  4. All of them expand variables