← Back to PHP Course | Chapter 6: Strings | Lesson 7 of 8

PHP String Operations

String operations are ways to glue words together and change them. Joining with a dot is like clicking two train cars together to make a longer train.
Syntax
php
$result = $first . $second;   // concatenation
$string .= "more text";         // append
strlen($string);
ucfirst($string);

Comparing Strings

PHP strings support the . (dot) operator for concatenation, letting you build up a longer string from smaller pieces, and .= to append onto an existing variable in place without retyping its name on the right side.

Example: Comparing Strings

php
<?php
// Declare `$greeting`, set to 'Hello'
$greeting = 'Hello';
$greeting .= ', World!';
// Print `$greeting` to the output
echo $greeting;
?>

Extracting Substrings

strlen() returns a string's length in bytes, which matters for multi-byte encodings like UTF-8 where a single visible character (say, an accented letter or emoji) can occupy more than one byte, so mb_strlen() is the safer choice for counting actual characters.

Example: Extracting Substrings

php
<?php
// Declare `$text`, set to "café"
$text = "café";
// Print `strlen($text) . "\n"` to the output
echo strlen($text) . "\n";
// Print `mb_strlen($text)` to the output
echo mb_strlen($text);
?>

Word Count Helper

Case-conversion functions like strtoupper(), strtolower(), and ucfirst() normalize text for comparisons or display, such as capitalizing the first letter of a name field before saving it or forcing a search query to lowercase for case-insensitive lookups.

Example: Word Count Helper

php
<?php
// Declare `$name`, set to "alice"
$name = "alice";
// Print `ucfirst($name) . "\n"` to the output
echo ucfirst($name) . "\n";
// Print `strtoupper($name)` to the output
echo strtoupper($name);
?>

Checking Prefix and Suffix

trim(), ltrim(), and rtrim() strip whitespace (or other specified characters) from the ends of a string, which is essential before validating or storing user input, since a stray leading space can silently break an equality check or a database lookup.

Example: Checking Prefix and Suffix

php
<?php
// Declare `$input`, set to "  [email protected]  "
$input = "  [email protected]  ";
// Print "[" . trim($input) . "]" to the output
echo "[" . trim($input) . "]";
?>

Shuffling and Hashing

str_pad() adds characters to a string until it reaches a target length, useful for formatting output like right-aligning numbers in a report or zero-padding an ID such as turning 7 into 007.

Example: Shuffling and Hashing

php
<?php
// Print `str_pad("7", 3, "0", STR_PAD_LEFT)` to the output
echo str_pad("7", 3, "0", STR_PAD_LEFT);
?>
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Comparing strings with == when strict type behavior matters, since "0" == false and other loose comparisons can give surprising results; use ===.
  2. Using substr($s, 5, 3) and forgetting that the second argument is the start index and the third is the length, not the end position.
  3. Forgetting that strlen counts bytes, so multibyte characters need mb_strlen.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.