PHP Strings

Beginner
8 min read
Updated: Nov 2026
5 examples
string type

Definition and Usage

In PHP, a string is text: a sequence of characters used for names, messages, HTML, file paths, and almost every piece of text a script prints or reads.

You create strings with single quotes, double quotes, or heredoc/nowdoc. Join them with the . operator. Built-in helpers such as strlen(), explode(), and htmlspecialchars() are global functions—you call them by name, not with object dot methods.

This page teaches what a PHP string is. The String Functions index lists every function guide in this series.

Single vs double quotes.

Double quotes expand variables ("Hi, $name"). Single quotes print $name literally. Prefer single quotes when you do not need expansion.

Assign the result.

Helpers return a new string. Write $upper = strtoupper($text);—calling the function alone does not change $text.

PHP Syntax

Common ways to create and work with strings:

PHP
$literal = 'Hello, PHP!';        // single quotes (literal)
$name    = "Ada";
$hello   = "Hi, $name!";          // double quotes expand $name
$full    = $first . ' ' . $last;  // . concatenation
$len     = strlen($text);         // byte length
$parts   = explode(',', $csv);    // split into array

Core Ideas

Ideas every PHP string tutorial builds on:

IdeaExampleRemember
Single quotes'Hello'Mostly literal text
Double quotes"Hi, $name"Variables and escapes expand
Concatenation$a . ' ' . $bDot ., not +
Lengthstrlen($s)Byte length (use mb_strlen for UTF-8 chars)
Split / joinexplode / implodeString ↔ array
Safe HTMLhtmlspecialchars($s)Escape before printing user text

PHP Examples

Five short scripts that define how strings work in a PHP tutorial.

Example 1 — Create and print

Assign text and display it with echo.

PHP
<?php
$language = 'PHP';
$version  = 8;

echo 'Language: ' . $language . "\n";
echo 'Version: ' . $version . "\n";
echo "Ready to learn strings!\n";
?>

How It Works

  1. $language stores a single-quoted literal.
  2. . joins strings (and converts the number).
  3. echo prints each line; "\n" adds a newline.

Example 2 — Single vs double quotes

See when variables expand and when they stay literal.

PHP
<?php
$name = 'Ada';

echo 'Hello, $name' . "\n";
echo "Hello, $name\n";
echo 'She said, \'Hi!\'' . "\n";
echo "Path: C:\\Users\\Dev\n";
?>

How It Works

  1. Single quotes keep $name as text.
  2. Double quotes insert the value of $name.
  3. \' and \\ escape quote and backslash.

Example 3 — Concatenation with . and .=

Build a message from smaller pieces.

PHP
<?php
$first = 'Hello';
$last  = 'PHP';

$greeting = $first . ', ' . $last . '!';
echo $greeting . "\n";

$status = 'Status: ';
$status .= 'Active';
echo $status . "\n";
?>

How It Works

  1. . joins two strings into a new one.
  2. .= appends onto the same variable.
  3. + is for numbers in PHP—not string join.

Example 4 — strlen() and indexing

Measure length and read a character by offset.

PHP
<?php
$word = 'Hello';

echo 'length: ' . strlen($word) . "\n";
echo 'first: ' . $word[0] . "\n";
echo 'last: ' . $word[strlen($word) - 1] . "\n";
?>

How It Works

  1. strlen returns the byte length.
  2. $word[0] is the first character (zero-based).
  3. UTF-8 multi-byte characters need mb_strlen / mb_substr.

Example 5 — explode() and implode()

Split CSV text and join it back together.

PHP
<?php
$csv  = 'apple,banana,cherry';
$tags = explode(',', $csv);

echo 'count: ' . count($tags) . "\n";
echo implode(' | ', $tags) . "\n";
?>

How It Works

  1. explode splits on a delimiter into an array.
  2. implode joins array items with glue.
  3. join() is an alias of implode().

Next: PHP String Functions Index

After you know what a string is, open the function index for full tutorials on every essential string helper in this PHP series.

Start withWhy
explode()Split text into an array
implode()Join array values into a string
htmlspecialchars()Escape HTML for safe output
addslashes()Escape quotes (know SQL alternatives too)

Full list: PHP String Functions index (/php/string/function).

Frequently Asked Questions

A string is a sequence of characters used for text—names, messages, HTML, and file paths. You write strings in single quotes, double quotes, or heredoc/nowdoc syntax.
Double-quoted strings expand variables and many escapes ("Hello, $name"). Single-quoted strings are mostly literal—only \\ and \' are special. Use single quotes when you do not need variable expansion.
Use the dot operator: $full = $first . ' ' . $last;. Append with .= : $text .= ' more';. echo can also print several pieces separated by commas.
No. Helpers like strtoupper(), trim(), and htmlspecialchars() return a new string. Assign the result: $safe = htmlspecialchars($raw);
Practice the examples on this page, then open the String Functions index for explode(), implode(), htmlspecialchars(), and every other function guide in this PHP tutorial series.

Browse every string function

Open the functions index for categorized guides with examples and FAQs.

String Functions Index →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful