SyntaxStudy
Sign Up
PHP Core String Functions
PHP Beginner 6 min read

Core String Functions

PHP ships with dozens of built-in string functions. The most-used ones let you measure, transform, and slice strings without writing any loops yourself.

  • strlen() returns the number of bytes in a string.
  • strtolower() / strtoupper() change case.
  • substr() extracts a portion of a string.
  • str_replace() replaces all occurrences of a search value.

These four functions alone cover the majority of everyday string work.

Example
<?php
$str = 'Hello, World!';

echo strlen($str);           // 13
echo strtolower($str);       // hello, world!
echo strtoupper($str);       // HELLO, WORLD!
echo substr($str, 7, 5);     // World
echo str_replace('World', 'PHP', $str); // Hello, PHP!

// substr with negative offset counts from the end
echo substr($str, -6);       // orld!  (last 6 chars... wait: World! = 6)
Pro Tip

Tip: substr() accepts a negative offset to count from the end of the string, and a negative length to omit characters from the end — very handy for trimming file extensions.