A PHP string is text in single or double quotes. Join strings with the dot operator, $first . ' ' . $last, or put a variable straight into double quotes: "Hello, $name".
The dot joins any two values into a new string. Numbers are converted for you, so "Total: " . 42 is "Total: 42".
Single vs double quotes
Double quotes do two extra things: they replace variables with their values, and they turn escape sequences such as \n (newline) and \t (tab) into the characters they stand for. Single quotes print the text exactly as written, except \' and \\.
Use single quotes for fixed text and double quotes when you want a variable or a \n inside. The speed difference between them is too small to measure, so pick whichever makes the line easier to read.
Concatenate strings with . and .=
.= appends to the end of a string variable. It is the usual way to build up text in a loop, for example a piece of HTML. Run this block and the editor opens the Page tab, which shows the list rendered.
Precedence matters when you mix . with arithmetic. Since PHP 8, + and - bind tighter than ., so "Sum: " . 2 + 3 is "Sum: 5". Parentheses make it obvious to the next reader: "Sum: " . (2 + 3).
Put variables, arrays and objects inside a string
A plain variable works on its own inside double quotes. For an array element, an object property chain or a method call, wrap the expression in curly braces. The braces also separate a variable from letters that follow it.
Methods work inside the braces, plain functions do not: the last line prints the braces and the function name around the value of $fruit. Call the function before the string, or use sprintf when the string has many values.
Change case: strtoupper, strtolower, ucfirst, ucwords
These functions work byte by byte and only change the letters a to z. For text with accents, Cyrillic or Greek, use the multibyte versions, which understand UTF-8:
Read single characters and repeat strings
A string can be indexed like an array. Negative offsets count from the end. For counting characters see strlen.
Common mistake: + instead of .
In JavaScript + joins strings. In PHP + is always arithmetic: numeric strings are added as numbers, and text that is not a number throws an error.
If you see a number where you expected joined text, or Unsupported operand types: string + string, look for a + that should be a ..
Frequently Asked Questions
How do you concatenate strings in PHP?
Use the dot operator: $full = $first . ' ' . $last;. To append to an existing string, use .=: $html .= '<li>Item</li>';. The + operator is for numbers, and "Hello" + "World" throws a TypeError in PHP 8.
What is the difference between single and double quotes in PHP?
Double quotes replace variables and escape sequences: "Hi $name\n" prints the value of $name and a newline. Single quotes print almost everything as written: 'Hi $name\n' prints the dollar sign, the name and a backslash followed by n. Inside single quotes only \' and \\ are escapes.
How do I put an array value inside a PHP string?
Wrap it in curly braces: "Hello, {$user['name']}". The same works for object properties and method calls: "{$user->getName()}". Without braces, "$user[name]" (no quotes around the key) also works for a simple key.
How do I make a string uppercase or lowercase in PHP?
strtoupper('php') returns PHP, strtolower('PHP') returns php, ucfirst('hello') capitalizes the first letter and ucwords('hello world') capitalizes every word. For text with accents or non-Latin letters use mb_strtoupper() and mb_strtolower().