An string is a sequence of characters we use to represent and manipulate text.
The web is, at its core, text. HTML is text, JSON is text, URLs are text. Therefore, mastering Strings is essential.
In PHP, a string is simply a series of characters, where each character is equivalent to one byte. But be careful, because in our language (Spanish), letters like ñ or accented letters occupy more than one byte. We’ll talk about that later.
Single quotes (') vs double quotes (")
This is the million-dollar question. Does it make a difference to use one or the other? Yes.
They are literal. What you write inside is exactly what comes out. PHP does not try to interpret anything.
$name = 'Luis';
echo 'Hello $name';
// Output: Hello $name
// (Does not substitute the variable)They are interpreted. PHP checks inside the string for variables or escape characters to process them.
$name = 'Luis';
echo "Hello $name";
// Output: Hello Luis
// (Substitutes the variable)Use single quotes by default for plain text, and double quotes if you need to insert variables inside.
Performance myth: it was once said that single quotes were much faster. In modern PHP, the difference is usually irrelevant compared to code clarity.
Variable interpolation
Inserting variables inside strings (Interpolation) is very convenient, but it has rules.
If you want to insert something more complex than a simple variable (like an array value or an object property), you must use curly braces {} to delimit it.
$user = ['name' => 'Ana', 'points' => 100];
// Wrong (May fail or give a syntax error)
echo "The user $user['name'] won";
// Correct (Complex Syntax / Curly Syntax)
echo "The user {$user['name']} won";The curly braces tell PHP: “Hey, everything inside here is the variable I want to print.” It’s an excellent practice for cleanliness.
Concatenation: the dot (.)
Unlike JavaScript or Python which use +, in PHP we join strings with the dot ..
$greeting = "Hello " . $name . ", welcome.";There is also the assignment and concatenation operator (.=), very useful for building long texts line by line:
$html = "<ul>";
$html .= "<li>Item 1</li>";
$html .= "<li>Item 2</li>";
$html .= "</ul>";Heredoc for multiline texts
Imagine you have to write a large block of HTML or a complex SQL query inside PHP.
The painful way:
$html = "<div class=\"card\">"; // Escaping quotes...
$html .= "<h1>" . $title . "</h1>"; // Concatenating...This is unreadable and error-prone.
The solution: Heredoc (<<<)
Heredoc allows you to write a multiline text block without worrying about escaping quotes, while maintaining the ability to use variables.
$title = "My Article";
$content = "Lorem ipsum...";
// EOD is an arbitrary identifier (End Of Data)
$html = <<<EOD
<div class="card">
<h1>$title</h1>
<p>$content</p>
<button onclick="alert('Hello')">Click</button>
</div>
EOD;Notice: I can use double quotes " and single quotes ' inside the HTML without escaping anything. It’s pure cleanliness.
Nowdoc: If you want the same thing but without interpreting variables (like giant single quotes), use <<<'EOD' (with single quotes around the identifier). Ideal for fixed code blocks or SQL.
Modern functions in PHP 8+
PHP 8 introduced functions with more expressive names for common text searches.
| Before (PHP 7 and lower) | Now (PHP 8+) | What does it do? |
|---|---|---|
strpos($t, 'x') !== false | str_contains($t, 'x') | Does the text contain it? |
substr($t, 0, 3) === 'Pre' | str_starts_with($t, 'Pre') | Does it start with…? |
substr($t, -3) === 'ing' | str_ends_with($t, 'ing') | Does it end with…? |
Use them. They make your code easier to read.
The ñ problem (multibyte strings)
PHP was created when English dominated the world. By default, string functions (strlen, substr) count bytes, not letters.
- The letter
aweighs 1 byte. - The letter
ñ(in UTF-8) weighs 2 bytes.
echo strlen('España');
// Result: 7 (Incorrect for humans!)
// E-s-p-a-ñ(2)-a = 7 bytesSolution:
To work in Spanish, always use the Multibyte versions of the functions, prefixed with mb_.
echo mb_strlen('España');
// Result: 6 (Correct)