📖 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
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
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
Hint
Use strtolower() inside ucfirst() to fix the case.
4
OUTPUT: Display the cleaned name
Use echo to print the value of cleanName.
PHP
Hint
Use echo $cleanName; to show the result.
Practice
(1/5)
1. Why are string functions important in PHP programming?
easy
A. They help manipulate text easily and avoid errors.
B. They make the program run faster by skipping text processing.
C. They automatically fix all spelling mistakes in strings.
D. They convert numbers into images.
Solution
Step 1: Understand the role of string functions
String functions provide tools to safely find, change, cut, and join text.
Step 2: Identify the benefit of using string functions
They help avoid errors and save time by handling text operations correctly.
Final Answer:
They help manipulate text easily and avoid errors. -> Option A
Quick Check:
String functions = easier text handling [OK]
Hint: String functions simplify text tasks and prevent mistakes [OK]
Common Mistakes:
Thinking string functions speed up programs by skipping processing