A String is a sequence of characters, and it is arguably the most frequently used data type in any programming language. From usernames and comments to HTML output, PHP offers hundreds of built-in functions to handle, modify, and analyze text efficiently.
1. Creating Strings
As learned in the “Data Types” chapter, strings can be created using single quotes (') or double quotes (").
| Quoting Style | Characteristic | Example |
Double Quotes (") | Allows variable parsing (interpolation). PHP looks for variables inside and replaces them with their values. | $name = "Alex"; echo "Hello, $name"; |
Single Quotes (') | Does NOT allow variable parsing. The string is taken literally. | $name = "Alex"; echo 'Hello, $name'; $\rightarrow$ Outputs: “Hello, $name” |
Best Practice: Use single quotes (') by default, as they are slightly faster since PHP doesn’t have to check for variables. Only use double quotes (") when you need variable interpolation.
2. Essential String Functions
These are the core functions you will use daily to get information about a string and manipulate it.
| Function | Purpose | Example | Result |
strlen() | Returns the length (number of characters) of a string. | strlen("PHP"); | 3 |
str_word_count() | Counts the number of words in a string. | str_word_count("Quick tutorial"); | 2 |
strrev() | Reverses the order of the characters in a string. | strrev("CODE"); | E D O C |
strpos() | Finds the position of the first occurrence of a substring within a string. | strpos("Hello PHP", "PHP"); | 6 |
str_replace() | Replaces all occurrences of a search string with a replacement string. | str_replace("dog", "cat", "I love my dog."); | “I love my cat.” |
Example: Analyzing User Input
<?php
$user_message = "The new update is fast and reliable.";
$word_count = str_word_count($user_message);
echo "Your message has: " . $word_count . " words.<br>";
// Replace a word in the message
$modified_message = str_replace("fast", "quick", $user_message);
echo "Modified: " . $modified_message;
?>
3. Substrings (Extracting Parts of Text)
A substring is a part of another string. The substr() function is essential for extracting a specific portion of text.
| Syntax | Description |
substr(string, start, length) | Extracts a segment starting at the start index, for an optional length. |
Example:
<?php
$email = "user123@domain.com";
// Start at index 0, extract the next 7 characters
$username = substr($email, 0, 7);
echo "Username: " . $username . "<br>"; // Output: user123
// Start at index 8 and go to the end (domain.com)
$domain = substr($email, 8);
echo "Domain: " . $domain;
?>
4. Case Conversion
These functions are used to ensure text is consistently capitalized or lowercased, which is useful for comparisons and formatting.
| Function | Purpose | Example |
strtolower() | Converts a string to lowercase. | strtolower("ALICE"); $\rightarrow$ “alice” |
strtoupper() | Converts a string to uppercase. | strtoupper("bob"); $\rightarrow$ “BOB” |
ucfirst() | Converts the first character of a string to uppercase. | ucfirst("welcome"); $\rightarrow$ “Welcome” |
Example: Standardization
<?php
$input_email = "User@EXAMPLE.com";
// Standardize input to lowercase for comparison
$standard_email = strtolower($input_email);
echo $standard_email;
?>
5. Stripping Whitespace
Whitespace (spaces, tabs, newlines) at the beginning or end of user input can cause problems when saving or comparing data.
| Function | Purpose | Example |
trim() | Removes whitespace from both the start and end of a string. | trim(" data "); $\rightarrow$ “data” |
ltrim() | Removes whitespace only from the left (start). | – |
rtrim() | Removes whitespace only from the right (end). | – |
Next Steps
You’ve mastered working with text. The next fundamental chapter should cover working with dates and times, which is essential for timestamps, scheduling, and display logic in web applications.
