PHP Strings

PHP Strings – Complete Beginner Guide With Examples
What is a String in PHP?
A string in PHP is a sequence of characters enclosed inside quotes. Strings are used to store and manipulate text such as names, messages, emails, and HTML content.
Example:
Output:
Strings are one of the most commonly used data types in PHP programming.
How to Create Strings in PHP
In PHP, strings can be created using:
Double quotes
" "Single quotes
' '
Double-Quoted Strings
Double quotes allow variable expansion (interpolation).
Example:
Output:
PHP replaces $name with its value.
Single-Quoted Strings
Single quotes do NOT allow variable expansion.
Example:
Output:
The variable is not processed.
Difference Between Single and Double Quotes
| Feature | Double Quotes | Single Quotes |
|---|---|---|
| Variable Expansion | Yes | No |
| Escape Sequences | Yes | Limited |
| Performance | Slightly Slower | Slightly Faster |
For dynamic content, use double quotes.
String Concatenation in PHP
Concatenation means joining strings together.
PHP uses the dot (.) operator.
Example:
Output:
String Length in PHP
To find the length of a string, use strlen().
Output:
It counts spaces as characters.
Counting Words in a String
Use str_word_count().
Output:
Replacing Text in a String
Use str_replace().
Output:
Changing Case of Strings
Convert to Uppercase
Output:
Convert to Lowercase
Output:
Removing Extra Spaces
Use trim() to remove spaces from beginning and end.
Finding Position of Text
Use strpos().
Output:
Extracting Part of a String
Use substr().
Output:
Escape Characters in PHP Strings
Escape characters allow special formatting.
Common escape sequences (in double quotes):
\n→ New line\t→ Tab\"→ Double quote\\→ Backslash
Example:
Multi-Line Strings (Heredoc Syntax)
PHP supports Heredoc for large text blocks.
Heredoc behaves like double quotes.
Nowdoc Syntax
Nowdoc behaves like single quotes.
Variables are not expanded.
String Comparison
Use == or ===.
This is case-sensitive.
For case-insensitive comparison, use:
Checking If String Contains Text
Always use !== false because position can be 0.
Converting String to Array
Use explode().
Converting Array to String
Use implode().
Security Tip – Escape Output
When displaying user input:
This prevents XSS attacks.
Always sanitize before outputting.
Real-World Example – Dynamic Greeting
Used in:
Login systems
Dashboards
E-commerce sites
Contact forms
Common Beginner Mistakes
Using + Instead of .
Wrong:
Correct:
Forgetting Variable Expansion Rules
Variables only expand inside double quotes.
Not Escaping User Input
Always use htmlspecialchars() for safety.
Why PHP Strings Are Important
Strings are essential for:
Handling form input
Displaying messages
Working with databases
Sending emails
Creating dynamic pages
Almost every PHP application uses strings heavily.
Frequently Asked Questions (FAQs)
1. What is a string in PHP?
A string in PHP is a sequence of characters enclosed inside single or double quotes. It is used to store and manipulate text data.
2. What is the difference between single and double quotes in PHP?
Double quotes allow variable expansion and escape sequences, while single quotes treat everything as plain text except escaped single quotes and backslashes.
3. How do you concatenate strings in PHP?
You can concatenate strings using the dot (.) operator in PHP.
4. How do you find the length of a string in PHP?
You can use the strlen() function to find the total number of characters in a string.
5. How can I replace text inside a string in PHP?
You can use the str_replace() function to search and replace text within a string.
Conclusion
PHP strings are powerful tools for handling text in web applications. By understanding:
String creation
Concatenation
Built-in functions
Case conversion
Searching and replacing
Security practices
You can confidently manipulate text in PHP.
Mastering strings is essential before moving to:
PHP arrays
PHP forms
PHP functions
PHP database operations
