PHP Strings

Working with text data in PHP

🔤 What are Strings?

Strings are sequences of characters used to store and manipulate text. PHP provides powerful functions to work with strings including concatenation, searching, replacing, and formatting operations.


<?php
$greeting = "Hello World!";
$name = 'Alice';
$message = "Welcome, $name!";

echo $message;
?>
                                    

Output:

Welcome, Alice!

String Operations

Concatenation

Join multiple strings together using the dot operator to create longer text from smaller pieces.

<?php
$first = "Hello";
$last = "World";
echo $first . " " . $last;
?>
📏

Length

Find the number of characters in a string using strlen() function for validation and processing.

<?php
$text = "Hello";
echo strlen($text); // 5
?>
🔍

Search

Find specific text within strings using strpos() to locate substrings and their positions.

<?php
$text = "Hello World";
echo strpos($text, "World");
?>
🔄

Replace

Substitute parts of strings with new text using str_replace() for dynamic content modification.

<?php
$text = "Hello World";
echo str_replace("World", "PHP");
?>

🔹 Creating Strings

Different ways to create strings in PHP:

<?php
// Single quotes - literal string
$single = 'Hello World';

// Double quotes - parses variables
$name = "Alice";
$double = "Hello $name";

// Concatenation
$concat = "Hello" . " " . "World";

// Heredoc syntax for multi-line
$heredoc = <<<EOT
This is a
multi-line string
with $name
EOT;

echo $single . "<br>";
echo $double . "<br>";
echo $concat;
?>

Output:

Hello World

Hello Alice

Hello World

🔹 String Length and Word Count

Measure string size and count words:

<?php
$text = "Hello World from PHP";

// String length
$length = strlen($text);
echo "Length: $length characters<br>";

// Word count
$words = str_word_count($text);
echo "Words: $words<br>";

// Get all words as array
$wordArray = str_word_count($text, 1);
echo "First word: " . $wordArray[0];
?>

Output:

Length: 20 characters

Words: 4

First word: Hello

🔹 String Search and Position

Find text within strings:

<?php
$text = "Hello World! Welcome to PHP World!";

// Find first occurrence
$pos = strpos($text, "World");
echo "First 'World' at position: $pos<br>";

// Find last occurrence
$lastPos = strrpos($text, "World");
echo "Last 'World' at position: $lastPos<br>";

// Check if string contains text
if (strpos($text, "PHP") !== false) {
    echo "Text contains 'PHP'";
}
?>

Output:

First 'World' at position: 6

Last 'World' at position: 28

Text contains 'PHP'

🔹 String Replace

Replace parts of strings with new text:

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

// Simple replace
$new = str_replace("World", "PHP", $text);
echo $new . "<br>";

// Replace multiple values
$original = "I like apples and oranges";
$replaced = str_replace(
    ["apples", "oranges"],
    ["bananas", "grapes"],
    $original
);
echo $replaced . "<br>";

// Case-insensitive replace
$text2 = "Hello WORLD!";
$new2 = str_ireplace("world", "PHP", $text2);
echo $new2;
?>

Output:

Hello PHP!

I like bananas and grapes

Hello PHP!

🔹 String Case Conversion

Change string letter casing:

<?php
$text = "Hello World";

// Uppercase
echo strtoupper($text) . "<br>";

// Lowercase
echo strtolower($text) . "<br>";

// First letter uppercase
echo ucfirst("hello world") . "<br>";

// First letter of each word uppercase
echo ucwords("hello world from php");
?>

Output:

HELLO WORLD

hello world

Hello world

Hello World From Php

🔹 String Trimming and Splitting

Remove whitespace and split strings:

<?php
// Trim whitespace
$text = "  Hello World  ";
echo "'" . trim($text) . "'<br>";

// Split string into array
$fruits = "apple,banana,orange";
$array = explode(",", $fruits);
echo "First fruit: " . $array[0] . "<br>";

// Join array into string
$words = ["Hello", "World", "PHP"];
$sentence = implode(" ", $words);
echo $sentence;
?>

Output:

'Hello World'

First fruit: apple

Hello World PHP

🧠 Test Your Knowledge

Which function returns the length of a string?