0
0
PHPprogramming~15 mins

Why string functions matter in PHP - See It in Action

Choose your learning style9 modes available
Why string functions matter
📖 Scenario: You are building a simple PHP program to handle user input for a website. Users enter their names, but sometimes they add extra spaces or use different letter cases. To make the names look neat and consistent, you need to use string functions.
🎯 Goal: Create a PHP script that cleans up a user's name by removing extra spaces and making the first letter uppercase and the rest lowercase.
📋 What You'll Learn
Create a variable called userName with the exact value " aLExAnDeR ".
Create a variable called cleanName that trims spaces from userName.
Use string functions to make the first letter uppercase and the rest lowercase in cleanName.
Print the final cleaned name using echo.
💡 Why This Matters
🌍 Real World
Cleaning and formatting user input is common in websites and apps to keep data consistent and readable.
💼 Career
Knowing string functions helps you prepare data for display, storage, or further processing in many programming jobs.
Progress0 / 4 steps
1
DATA SETUP: Create the initial string variable
Create a variable called userName and set it to the string " aLExAnDeR " exactly.
PHP
Need a hint?

Use $userName = " aLExAnDeR "; to create the variable.

2
CONFIGURATION: Trim spaces from the string
Create a variable called cleanName and set it to the result of trimming spaces from userName using the trim() function.
PHP
Need a hint?

Use trim($userName) to remove spaces from both ends.

3
CORE LOGIC: Format the string with proper case
Update cleanName to have the first letter uppercase and the rest lowercase using ucfirst() and strtolower() functions.
PHP
Need a hint?

Use strtolower() inside ucfirst() to fix the case.

4
OUTPUT: Display the cleaned name
Use echo to print the value of cleanName.
PHP
Need a hint?

Use echo $cleanName; to show the result.