All posts
PHPNovember 6, 20267 min read

PHP String Functions for Beginners: Easy Guide

PHP string functions for beginners: learn strlen, str_replace, strpos, substr, trim and more with tiny examples explained line by line. Start learning today.

F
Fepiq Team
Fepiq

PHP string functions are built-in tools that let you measure, change, search and clean up text. A string is just text inside quotes, like "Hello". If you are searching for php string functions for beginners, here is the short answer: use strlen() to count characters, strtoupper() and strtolower() to change case, str_replace() to swap words, strpos() to find text, substr() to cut out a part, and trim() to remove extra spaces.

This guide explains each one with a tiny example you can copy and run. You do not need any experience. Every new word is explained the first time it appears.

What is a string in PHP?

A string is a piece of text. In PHP you write it between quotes. Single quotes and double quotes both work. A function is a ready-made helper with a name. You give it some input (inside round brackets), and it gives you a result back.

php
<?php
$name = "Maya";
$greeting = 'Welcome to PHP';
echo $name;
echo "\n";
echo $greeting;

Line 2 creates a variable called $name. A variable is a labeled box that holds a value. Line 3 stores another string. The echo command prints a value to the screen. The \n in line 5 means 'start a new line'. Output: Maya, then Welcome to PHP on the next line.

Count characters with strlen()

strlen() tells you how many characters are in a string. This is handy for checking that a password or username is long enough.

php
<?php
$word = "Hello World";
echo strlen($word);

Line 2 stores the text. Line 3 passes it to strlen() and prints the answer: 11. The space counts as one character, so 5 + 1 + 5 = 11.

Change the case of text

Use strtoupper() for capital letters, strtolower() for small letters, ucfirst() to capitalize only the first letter, and ucwords() to capitalize the first letter of every word.

php
<?php
$text = "learn php today";
echo strtoupper($text) . "\n";
echo strtolower("LOUD TEXT") . "\n";
echo ucfirst($text) . "\n";
echo ucwords($text);

The dot (.) joins two strings together. Line 3 prints LEARN PHP TODAY. Line 4 prints loud text. Line 5 prints Learn php today. Line 6 prints Learn Php Today. The original $text never changes. Functions return a new string instead.

Find text with strpos()

strpos() looks for a smaller piece of text inside a bigger one and returns its position. Positions start counting at 0, not 1. If the text is not found, it returns false.

php
<?php
$sentence = "I love PHP";
$pos = strpos($sentence, "love");
if ($pos !== false) {
    echo "Found at position " . $pos;
} else {
    echo "Not found";
}

Line 3 searches for 'love'. The letters I, space, then l: 'love' starts at position 2, so the output is 'Found at position 2'. We use !== false (not equal and not the same type) because a match at position 0 would otherwise look like false. In PHP 8 you can also use str_contains($sentence, "love"), which simply returns true or false.

Replace text with str_replace()

str_replace() swaps one piece of text for another. It takes three things: what to find, what to put instead, and the original string.

php
<?php
$message = "I like cats. Cats are great.";
echo str_replace("cats", "dogs", $message);

Line 3 replaces 'cats' with 'dogs'. The output is 'I like dogs. Cats are great.' Notice the capital 'Cats' stayed the same, because str_replace() is case-sensitive. For a case-insensitive swap, use str_ireplace().

Cut out a part with substr()

substr() returns a slice of a string. Give it the string, the start position, and optionally how many characters to take.

php
<?php
$code = "ABCDEFG";
echo substr($code, 0, 3) . "\n";
echo substr($code, 2, 2) . "\n";
echo substr($code, -2);

Line 3 starts at position 0 and takes 3 characters: ABC. Line 4 starts at position 2 and takes 2: CD. Line 5 uses a negative start, which counts from the end, so it prints FG.

Remove extra spaces with trim()

Users often type spaces before or after their input by accident. trim() removes spaces (and line breaks) from both ends of a string. It is a great first step when handling form data.

php
<?php
$email = "   sam@example.com  ";
$clean = trim($email);
echo "[" . $clean . "]";

Line 3 removes the extra spaces and saves the result in $clean. Line 4 prints it inside square brackets so you can see there is no space left: [sam@example.com]. ltrim() trims only the left side and rtrim() only the right.

Split and join: explode() and implode()

explode() cuts a string into an array (a list of values) at a separator you choose. implode() does the opposite and joins a list into one string.

php
<?php
$tags = "php,sql,react";
$list = explode(",", $tags);
echo $list[1] . "\n";
echo implode(" | ", $list);

Line 3 splits at every comma, giving a list of three items. Line 4 prints the item at index 1, which is sql (lists also start at 0). Line 5 joins them back with ' | ' between each: php | sql | react.

Quick reference table

FunctionWhat it doesExample result
strlen("Hi")Counts characters2
strtoupper("hi")All capital lettersHI
ucwords("a b")Capitalize each wordA B
strpos("cat","a")Finds position (starts at 0)1
str_replace("a","o","cat")Replaces textcot
substr("hello",1,3)Cuts out a partell
trim(" hi ")Removes edge spaceshi
explode(",","a,b")String to array["a","b"]

Common beginner mistakes

  • Forgetting that positions start at 0, not 1.
  • Expecting a function to change the original variable. Save the result in a variable instead.
  • Using == false instead of !== false after strpos().
  • Forgetting that str_replace() is case-sensitive.
  • Mixing up the argument order. For strpos() the big string comes first; for explode() the separator comes first.

Frequently asked questions

What are string functions in PHP?+

They are built-in helpers that work on text. They can count characters, change case, search, replace, cut and clean strings. PHP has more than 100 of them, but about ten cover most beginner needs.

How do I check if a string contains a word in PHP?+

In PHP 8 and newer, use str_contains($text, "word"), which returns true or false. In older versions, use strpos($text, "word") !== false.

How do I remove spaces from a string in PHP?+

Use trim() to remove spaces from the start and end. To remove every space, including the middle ones, use str_replace(" ", "", $text).

What is the difference between single and double quotes in PHP?+

Double quotes let you place variables inside the text, like "Hello $name", and understand special codes such as \n. Single quotes print the text exactly as written.

Are PHP string functions case-sensitive?+

Most are. strpos() and str_replace() treat A and a as different letters. Use stripos() and str_ireplace() when you want to ignore case.

Next, learn how to run code many times with loops, which pairs well with string work.

Read: PHP loops for beginners

Want help building your first PHP project or website? Our team is happy to guide you.

Talk to Fepiq

Get new posts in your inbox

Occasional, no-fluff notes on shipping modern software — startups, automation, Laravel, Shopify and more. No spam, unsubscribe anytime.

Keep reading

Related posts

All posts

Let's build something

Ready to ship your next product with Fepiq?

Book a free discovery call. We'll listen, ask sharp questions, and send you a proposal within 3 business days.