PHPLesson 6 of 13

Strings and String Functions

Strings are the text of your program: names, messages, HTML, file paths. PHP gives you a toolbox of functions to measure, cut, join, and search them. You do not need to remember them all, just the common ones and the idea that the toolbox exists.

Strings are the text of your program: names, messages, HTML, file paths. PHP gives you a toolbox of functions to measure, cut, join, and search them. You do not need to remember them all, just the common ones and the idea that the toolbox exists.

Joining strings uses the dot operator. $a . $b makes one string from two. This is concatenation, and it is how you build sentences from variables. Double-quoted strings also interpolate variables directly, which often reads better than a chain of dots.

The most used functions are few. strlen() measures, str_replace() swaps text, strtolower() and strtoupper() change case, trim() strips whitespace, and explode() splits a string into an array by a separator. Their reverse partner implode() joins an array back into a string.

Dates are strings with rules. date() formats the current time, and strtotime() parses almost any date text into a timestamp. PHP stores dates as Unix timestamps, seconds since 1970, and the functions translate between that number and human text.

explode and implode

explode(";", $text) cuts a string into an array wherever the separator appears. implode(";", $parts) does the reverse. This pair moves data between string form and list form, which you will need for CSV lines and repeated fields.

PHP
$line = "apple;banana;cherry";
$parts = explode(";", $line);
print_r($parts); // 3 items

$back = implode(", ", $parts);
echo $back; // apple, banana, cherry

Date formatting

date() reads format letters: Y is the four-digit year, m the month, d the day, H the hour. Combine them with separators to build any date string. strtotime() accepts phrases like "next monday" and returns a timestamp.

PHP
echo date("Y-m-d");            // 2026-08-20
echo date("l, F jS");         // Thursday, August 20th
echo date("H:i");            // 14:35
echo date("Y-m-d", strtotime("next monday"));

Syntax

PHP
<?php
$text = "  Hello, World!  ";
echo strlen($text);          // 17 including spaces
echo trim($text);            // "Hello, World!"
echo str_replace("World", "PHP", $text);
?>

Single vs Double Quotes Matter for $var

Single quotes don't expand $var — "hi $name" works, 'hi $name' shows literal $name.

PHP
"Hello $name" // expands

Good to know

  • The dot operator joins strings; double quotes interpolate variables.
  • strlen, str_replace, trim, and explode cover most daily needs.
  • explode splits by a separator, implode joins an array back up.
  • date() formats timestamps with letters like Y, m, d, H.
Try it Yourself Edit the code, press Run, then check what changed in the Result.
Ready
Code
Result

Practice makes it stick

Open the Playground and build your own page with the tags you just learned.

→ Open Playground