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.
PHP string functions for beginners: learn strlen, str_replace, strpos, substr, trim and more with tiny examples explained line by line. Start learning today.
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.
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
$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.
strlen() tells you how many characters are in a string. This is handy for checking that a password or username is long enough.
<?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.
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
$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.
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
$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.
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
$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().
substr() returns a slice of a string. Give it the string, the start position, and optionally how many characters to take.
<?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.
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
$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.
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
$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.
| Function | What it does | Example result |
|---|---|---|
| strlen("Hi") | Counts characters | 2 |
| strtoupper("hi") | All capital letters | HI |
| ucwords("a b") | Capitalize each word | A B |
| strpos("cat","a") | Finds position (starts at 0) | 1 |
| str_replace("a","o","cat") | Replaces text | cot |
| substr("hello",1,3) | Cuts out a part | ell |
| trim(" hi ") | Removes edge spaces | hi |
| explode(",","a,b") | String to array | ["a","b"] |
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.
In PHP 8 and newer, use str_contains($text, "word"), which returns true or false. In older versions, use strpos($text, "word") !== false.
Use trim() to remove spaces from the start and end. To remove every space, including the middle ones, use str_replace(" ", "", $text).
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.
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 beginnersWant help building your first PHP project or website? Our team is happy to guide you.
Talk to FepiqOccasional, no-fluff notes on shipping modern software — startups, automation, Laravel, Shopify and more. No spam, unsubscribe anytime.
Keep reading
Learn SQL AND, OR and NOT operators with simple examples. See how to combine conditions in WHERE and avoid the common mistakes beginners make.
Learn JavaScript template literals with simple examples: backticks, ${} variables, and multi-line strings. A beginner-friendly guide you can copy and run.
Let's build something
Book a free discovery call. We'll listen, ask sharp questions, and send you a proposal within 3 business days.