Free tutorials & notes in Hindi & English · Clean code examples 🧰 Free PDF & Image Tools →
🟡 Control Flow  ·  Lesson 11

PHP String Functions (with Output)

In this lesson: Most-used PHP string functions — strlen, strtoupper, substr, strpos, explode, implode, str_replace — with output.

Working with text

A lot of web work is about text — names, emails, messages, search terms. PHP gives you many ready-made string functions to measure, change, search and split text without writing loops yourself. Learn a handful of these and most text tasks become one line.

💡 In string functions, character positions start at 0 — the first letter is position 0.

Length and case

<?php
  $t = "Coding Easily";
  echo strlen($t);        // 13  (number of characters)
  echo strtoupper($t);    // CODING EASILY
  echo strtolower($t);    // coding easily
  echo ucwords("hello world"); // Hello World
?>
13 CODING EASILY coding easily Hello World

strlen counts characters, strtoupper/strtolower change case, and ucwords capitalises the first letter of each word.

<?php
  $t = "I like PHP";
  echo str_replace("PHP", "MySQL", $t);   // I like MySQL
  echo strpos($t, "PHP");                  // 7 (position where PHP starts)
?>
I like MySQL
7

str_replace swaps text; strpos tells you where a word starts (or returns false if not found). Because 0 is a valid position, check strpos with === false.

Trim and substring

<?php
  $name = "   Aman   ";
  echo trim($name);              // "Aman" (spaces removed)
  echo substr("CodingEasily", 0, 6);  // Coding
?>
Aman
Coding

trim removes stray spaces from both ends — essential for cleaning form input. substr pulls out a part: start at 0, take 6 characters.

explode and implode

These convert between a string and an array — very useful for CSV-like data.

<?php
  $csv = "apple,mango,banana";
  $arr = explode(",", $csv);   // ["apple","mango","banana"]
  echo $arr[1];                // mango

  $back = implode(" | ", $arr);
  echo $back;                  // apple | mango | banana
?>
mango
apple | mango | banana

explode splits a string into an array by a separator; implode joins an array back into a string. They are perfect opposites.

Common mistakes

  • Forgetting positions start at 0. The first character is substr($t, 0, 1), not position 1.
  • Checking strpos with ==. Since it can return 0 (a valid position), use === false to test "not found".
  • Not trimming form input. Extra spaces cause failed logins and duplicate data.
  • Mixing up explode and implode. explode makes an array; implode makes a string.

Summary

  • strlen for length; strtoupper/strtolower/ucwords for case.
  • str_replace to replace; strpos to find a position.
  • trim cleans spaces; substr pulls out a part.
  • explode splits to an array; implode joins to a string.
  • Positions start at 0 — remember this for substr and strpos.

Next, store split results with arrays in PHP, or clean user input in form validation.

Frequently Asked Questions

How do I find the length of a string in PHP?
Use strlen($text). It returns the number of characters, so strlen("PHP") gives 3. It is often used to check if input is too short or too long.
How do I join and split strings?
Use explode(",", $csv) to split a string into an array by a separator, and implode(",", $array) to join an array back into a string. They are exact opposites of each other.
How do I replace part of a string?
Use str_replace("old", "new", $text). It finds every "old" and swaps it for "new". It is great for cleaning or reformatting text.
What does trim() do?
trim($text) removes spaces (and other whitespace) from the start and end of a string. It is very important for form input, where users often add accidental spaces.
How do I get part of a string?
Use substr($text, start, length). For example substr("CodingEasily", 0, 6) gives "Coding". Counting starts at 0 for the first character.
← Back to PHP Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.
Next topicPHP Forms ($_POST / $_GET) Your progress: 11 / 50 (22%) · Lesson 12 / 50 Continue →

How was this lesson?

Your feedback helps us make the content better.

Write a review or suggestion (optional)

After approval, your name and city may appear publicly with your review. Please do not write your email or phone number.

▶ CodingEasily on YouTube

Learn with video lessons in easy Hindi

Featured video: RDBMS Part 3 – Class XI Commerce IT

▶ Subscribe – it’s free