Strings and Type Casting in PHP
In real-world PHP applications, most of the data you work with is text — user names, emails, form inputs, and messages. At the same time, PHP often needs to convert that data into the correct format for calculations or logic.
In this lesson, you’ll learn how strings work in PHP, how to manipulate them using built-in functions, and how type casting helps you safely convert data between types.
What is a String in PHP?
A string is a sequence of characters used to store text. PHP allows you to create strings using either single quotes (' ') or double quotes (" ").
$name = "Rohan"; $message = 'Welcome to MasterPHP';
Single Quotes vs Double Quotes
The key difference is how PHP handles variables inside strings.
Double Quotes (Variables are parsed)
$name = "Rohan"; echo "Hello $name"; // Output: Hello Rohan
When to use:
When you want to include variables directly inside a string.
Single Quotes (No variable parsing)
$name = "Rohan"; echo 'Hello $name'; // Output: Hello $name
When to use:
When you want a raw string without variable substitution.
String Concatenation
Concatenation means joining multiple strings together using the dot (.) operator.
$firstName = "Rohan"; $lastName = "Rajbhar"; echo $firstName . " " . $lastName; // Output: Rohan Rajbhar
Use case:
Combining dynamic values like user names, messages, or formatted outputs.
Common String Functions in PHP
strlen() — Get String Length
echo strlen("Hello"); // Output: 5
Use case:
Useful for input validation, such as checking minimum password length.
str_replace() — Replace Text
echo str_replace("World", "PHP", "Hello World"); // Output: Hello PHP
Use case:
Cleaning or modifying user input, such as replacing unwanted words or formatting text.
strtolower() — Convert to Lowercase
echo strtolower("HELLO"); // Output: hello
Use case:
Standardizing user input for consistent comparison (e.g., emails or usernames).
strtoupper() — Convert to Uppercase
echo strtoupper("hello"); // Output: HELLO
Use case:
Formatting output or enforcing consistent display styles.
What is Type Casting in PHP?
Type casting is the process of converting a variable from one data type to another. This is important because PHP often receives data (like form input) as strings, even when you need numbers for calculations.
Type Casting Syntax
$number = (int) "10";
Common Type Casting Types (With Use Cases)
Integer Casting (int)
$value = (int) "25";
Why:
Form inputs are always strings, so you convert them to integers before performing calculations.
Float Casting (float)
$value = (float) "10.5";
Why:
Useful when working with prices, percentages, or decimal values from APIs or databases.
String Casting (string)
$value = (string) 100;
Why:
Needed when displaying numeric data as text or combining it with other strings.
Boolean Casting (bool)
$value = (bool) 1;
Why:
Used in conditions, such as checking if a value exists or evaluating truthy/falsey inputs.
Common Mistakes Beginners Make
1. Using + Instead of . for Strings
echo "Hello" + "World"; // Incorrect
Why it fails:
PHP tries to convert strings to numbers, resulting in 0.
2. Confusing == and ===
var_dump("10" == 10); // true
var_dump("10" === 10); // false
Why it matters:
== compares values loosely, while === checks both value and type.
3. Unexpected Boolean Casting
$value = (bool) "false"; // true
Why it happens:
Any non-empty string is treated as true in PHP.
Real-World Example (Strings + Type Casting Together)
$userName = " rohan "; $orderAmount = "150"; // Clean string input $userName = trim($userName); $userName = ucfirst(strtolower($userName)); // Convert amount to integer $orderAmount = (int)$orderAmount; // Output message echo "Hello " . $userName . ", your order total is ₹" . $orderAmount;
Output:
Hello Rohan, your order total is ₹150
What this shows:
- String cleaning (
trim,strtolower,ucfirst) - Concatenation
- Type casting for calculations/output
Practice Exercise
Task 1
Create $firstName and $lastName, then combine them.
Expected Output:
Rohan Rajbhar
Task 2
Print a sentence using both single and double quotes.
Expected Output:
Hello Rohan Hello $name
Task 3
Convert "200" into an integer and add 50.
Expected Output:
250
Summary
In this lesson, you learned how to work with strings in PHP, including how to create, combine, and manipulate them using built-in functions. You also learned how type casting allows you to safely convert data types, which is essential when working with user input and real-world applications.