PHPStrings

Strings

A PHP string is a sequence of bytes — nothing more mystical than that. There's no separate "character" type like in some languages; a single letter and a 10-megabyte file you just read into memory are both just strings. This byte-oriented design is simple and fast, but it has real consequences once you start working with UTF-8 text containing accented letters or emoji, which is covered later. For now, think of a string as raw bytes with a length, and everything else follows from that.

Creating strings

You can build a string with single quotes, double quotes, or the heredoc/nowdoc syntax (its own dedicated page). The simplest form is single or double quotes wrapped around text:

basic-strings.php

PHP
<?php

$city = 'Toronto';
$greeting = "Hello, world!";
$empty = '';

var_dump($city);
var_dump($empty);
string(7) "Toronto"
string(0) ""

Notice that var_dump reports the byte length right in its output — string(7) for 'Toronto', because each of those seven ASCII characters occupies exactly one byte. That number is the same value strlen() would return.

Concatenation: joining strings together

PHP uses a dot (.) as its concatenation operator — not +, which is reserved for arithmetic. To glue two or more strings into one, put a . between them. The compound assignment form .= appends onto an existing variable, which is the idiomatic way to build up a string piece by piece inside a loop.

concatenation.php

PHP
<?php

$first = 'Ada';
$last = 'Lovelace';

$fullName = $first . ' ' . $last;
echo $fullName;
echo PHP_EOL;

$report = 'Items: ';
foreach (['apple', 'bread', 'milk'] as $item) {
    $report .= $item . ', ';
}
$report = rtrim($report, ', ');

echo $report;
Ada Lovelace
Items: apple, bread, milk
Dot, not plus
Writing `$first + $last` will not concatenate anything — PHP tries to coerce both operands into numbers for addition, which for two words evaluates to `0` (and, on PHP 8, throws a `TypeError` for genuinely non-numeric strings in stricter contexts). Reach for `.` and `.=` whenever you mean "join these strings," and save `+` for actual arithmetic.
Measuring length with strlen()

strlen() returns the number of bytes in a string. For plain ASCII text, that's the same as the number of visible characters, which is why it's tempting to treat "length" and "character count" as interchangeable — a habit that breaks down for multibyte text (see the multibyte strings page).

strlen-basics.php

PHP
<?php

echo strlen('PHP');        // 3
echo PHP_EOL;
echo strlen('Hello, PHP'); // 10, including the comma and space
echo PHP_EOL;
echo strlen('');           // 0
3
10
0
Accessing individual characters by index

Strings support array-style bracket access, $str[0], to read a single byte at a zero-based position. Negative indexes count from the end starting at -1, a convenience added properly in PHP 7.1+. Reading out of range returns an empty string with a warning; it does not throw.

char-access.php

PHP
<?php

$word = 'Laravel';

echo $word[0];   // 'L'
echo PHP_EOL;
echo $word[3];   // 'a'
echo PHP_EOL;
echo $word[-1];  // 'l' — last character
echo PHP_EOL;
echo strlen($word); // 7
L
a
l
7
Strings are immutable values, even when they look mutable

This is a subtle but important point: PHP strings are copy-on-write value types, not mutable buffers you edit in place. When you write $word[0] = 'B', PHP doesn't reach into the existing string and overwrite a byte in shared memory — it produces a brand-new string value and rebinds $word to it. If another variable was pointing at the same underlying string, that other variable is completely unaffected, because assignment in PHP always copies scalar values.

immutability.php

PHP
<?php

$original = 'Cats';
$copy = $original;

$original[0] = 'B';

echo $original; // Bats
echo PHP_EOL;
echo $copy;     // Cats — untouched, because $copy holds its own value
Bats
Cats
  • A PHP string is a byte sequence, not a list of "characters" in the Unicode sense.

  • Use . to concatenate and .= to append onto an existing variable.

  • strlen() counts bytes, which equals character count only for single-byte encodings like ASCII.

  • $str[n] reads a single byte by position; negative indexes count from the end.

  • Assigning one string variable to another always copies the value — there is no shared, mutable buffer.

Tip
When you see unexpected behavior with `$str[n] = ...`, remember it creates a new string rather than mutating one in place — that mental model explains why loops that repeatedly index into and rebuild a large string are slower than building pieces in an array and joining them once with `implode()` at the end.