Strings & String Functions

Text is the lifeblood of web apps — names, emails, URLs, messages. This lesson covers building strings, searching them, slicing them, splitting and joining, formatting with sprintf , and the multibyte functions you need for real-world Unicode text.

Learn Strings & String Functions in our free PHP course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick recall.

Part of the free Php course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

What You'll Learn in This Lesson

1️⃣ Building Strings

There are four ways to assemble text, and you'll mix them constantly. The dot operator glues strings together. Double quotes interpolate variables. Curly braces like {' '} remove any ambiguity about where a variable name ends. And sprintf() builds precisely formatted output from placeholders — ideal for money and padded numbers.

The %0.2f in sprintf means "a float with exactly two decimal places", which is why 4.5 prints as 4.50 — perfect for currency.

2️⃣ Length, Case & Trimming

The day-to-day toolkit: strlen() for length, strtoupper() / strtolower() for case, ucfirst() / ucwords() for capitalisation, and trim() to strip stray whitespace — which you should run on essentially every piece of user input.

3️⃣ Searching Within Strings

To ask "does this contain…?", PHP 8 gives you three readable booleans: str_contains() , str_starts_with() , and str_ends_with() . When you need the actual position, strpos() returns an index — but watch the classic trap below.

The trap: if a match is at the very start, strpos returns 0 , which is falsy. Always compare with === false to tell "not found" apart from "found at index 0".

4️⃣ Slicing, Replacing, Splitting

substr() extracts a slice (negative offsets count from the end). str_replace() swaps every occurrence. And the explode() / implode() pair converts between a delimited string and an array — the bread and butter of parsing CSV rows, paths, and tags.

5️⃣ Unicode: The mb_* Functions

Here's the gotcha that bites every PHP developer eventually: the plain string functions count bytes , not characters. Real text — names with accents, emoji, non-Latin scripts — is UTF-8, where one character can be several bytes. For correctness, use the multibyte equivalents: mb_strlen , mb_substr , mb_strtoupper .

Now you try. Fill the ___ blanks to clean up and reformat a username.

These scrambled lines should take a full name and print initials. Put them in the order that produces G.H.

You must define $name first, then split it into $parts with explode , then build $initials from those parts (a string indexed with [0] gives its first character), and only then echo. Output: G.H.

Predict the output before revealing each answer.

Prints 5 . The é is two bytes in UTF-8, and strlen counts bytes. Use mb_strlen to get the character count 4 .

Prints ef . A negative start counts from the end of the string, so -2 means "the last two characters".

Prints bool(false) . The match is at index 0, so strpos returns 0 — which is not identical to false . This is exactly why you compare with === false .

📋 Quick Reference — String Functions

Write it yourself, run it on onecompiler.com/php or your own machine, then check against the expected output in the comments.

Practice quiz

What does strlen() actually count?

  • Characters
  • Words
  • Bytes
  • Lines

Answer: Bytes. strlen counts bytes, not characters — multibyte (UTF-8) letters take 2-4 bytes each.

What does echo strlen("café"); print?

  • 3
  • 4
  • 5
  • 6

Answer: 5. The é is two bytes in UTF-8, so strlen returns 5. mb_strlen would return 4.

Which function correctly counts the number of characters in Unicode text?

  • strlen
  • mb_strlen
  • count
  • strpos

Answer: mb_strlen. mb_strlen (and the mb_* family) handle multibyte characters correctly.

What does echo substr("ORD-2026-USA", -3); print?

  • ORD
  • USA
  • 2026
  • -US

Answer: USA. A negative start counts from the end, so -3 returns the last three characters: USA.

Why must you compare strpos() with === false?

  • strpos is very slow
  • A match at index 0 returns 0, which is falsy
  • strpos returns a string
  • It only works on numbers

Answer: A match at index 0 returns 0, which is falsy. A match at the start gives index 0; 0 is falsy, so === false tells 'not found' apart from 'found at 0'.

Which PHP 8 function returns a clean boolean for 'does it contain X?'

  • strpos
  • str_contains
  • substr
  • explode

Answer: str_contains. str_contains returns a boolean and reads like plain English.

What does explode("-", "ORD-2026-USA") return?

  • The string "ORD2026USA"

explode splits a string on the separator into an array.

Which function joins an array back into a single string?

  • implode
  • explode
  • str_split
  • trim

Answer: implode. implode(glue, array) joins array elements into one string.

What does trim(" spaced out ") return?

  • "spacedout"
  • "spaced out" (no surrounding spaces)
  • An error
  • The original with spaces

Answer: "spaced out" (no surrounding spaces). trim removes whitespace from both ends, leaving "spaced out".

What does sprintf("%0.2f", 4.5) produce?

  • "4"
  • "4.5"
  • "4.50"
  • "450"

Answer: "4.50". %0.2f formats a float with exactly two decimal places, giving "4.50".