← Back to PHP Course | Chapter 6: Strings | Lesson 1 of 8

PHP Strings Introduction

A string is a piece of text, like a word or a sentence written on a ribbon. PHP lets you write it in single quotes, which keep it plain, or double quotes, which fill in variables.
Syntax
php
$single = 'text, $variable is not replaced';
$double = "text, $variable is replaced";
$joined = $first . " " . $second;
$joined .= " more text";

Single Quoted Strings

Single-quoted strings do the least amount of work: they don't evaluate $variable references or process most escape sequences, printing almost everything exactly as typed.

That simplicity makes them marginally faster and the right choice for plain, static text.

Example: Single Quoted Strings

php
<?php
// Declare `$name`, set to "Alice"
$name = "Alice";
// Print 'Hello, $name' to the output
echo 'Hello, $name';
?>

Double Quoted Strings

Double-quoted strings actively process their contents: any $variable inside is replaced with its current value (interpolation), and escape sequences like \n and \t are converted into a real newline or tab rather than printed as literal backslash-n.

Example: Double Quoted Strings

php
<?php
// Declare `$name`, set to "Alice"
$name = "Alice";
// Print "Hello, $name\n" to the output
echo "Hello, $name\n";
?>

String Concatenation

The . operator joins two strings end-to-end into one — $greeting = 'Hello, ' . $name; — and .= does the same thing while also reassigning the result back into the left-hand variable, appending onto whatever it already contained.

Example: String Concatenation

php
<?php
// Declare `$name`, set to "Alice"
$name = "Alice";
// Declare `$greeting`, set to `'Hello, ' . $name`
$greeting = 'Hello, ' . $name;
// Print `$greeting` to the output
echo $greeting;
?>

Checking String Length

strlen() returns the total number of bytes in a string, counting every character including spaces and punctuation.

For strings containing multi-byte characters (accented letters, emoji), strlen() counts bytes rather than visible characters, which is worth knowing before you rely on it for display-length logic.

Example: Checking String Length

php
<?php
// Declare `$text`, set to "Hello!"
$text = "Hello!";
// Print `strlen($text)` to the output
echo strlen($text);
?>

Accessing Single Characters

You can read (or overwrite) a single character in a string using square-bracket indexing, exactly like an array — $word[0] gets the first character. PHP strings are zero-indexed, so the first character sits at position 0, not 1.

Example: Accessing Single Characters

php
<?php
// Declare `$word`, set to "Hello"
$word = "Hello";
// Print `$word[0]` to the output
echo $word[0];
?>
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Using single quotes and expecting variables to be replaced, so 'Hello $name' prints $name literally.
  2. Using + to join strings, which does arithmetic in PHP; use . instead.
  3. Using strlen() on multi-byte text like "é" and expecting characters, when it counts bytes; use mb_strlen().

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.