Lesson 5 of 20

Strings

String Functions

PHP has a rich set of built-in string functions for manipulation and formatting.

Example
<?php
$text = "Hello, World!";

echo strlen($text);           // 13
echo strtoupper($text);       // "HELLO, WORLD!"
echo strtolower($text);       // "hello, world!"
echo substr($text, 0, 5);     // "Hello"
echo str_replace("World", "PHP", $text); // "Hello, PHP!"
echo strpos($text, "World");  // 7
echo trim("  hello  ");       // "hello"
echo str_repeat("*", 5);      // "*****"
echo ucfirst("hello world");  // "Hello world"
echo ucwords("hello world");  // "Hello World"

// String interpolation (double quotes only)
$name = "Alice";
echo "Hello, $name!";         // "Hello, Alice!"
echo "Age: {$user['age']}";   // with complex expressions
echo 'No $interpolation';     // literal: 'No $interpolation'

// Heredoc
$html = <<<HTML
<div>
    <h1>Hello, $name</h1>
</div>
HTML;
?>