📖 Scenario: You are creating a simple PHP script to format and display information about a product in a neat way. This is useful when you want to show prices, quantities, and names clearly on a webpage or in logs.
🎯 Goal: Build a PHP script that uses printf and sprintf to format product details like name, price, and quantity with specific formatting rules.
📋 What You'll Learn
Create variables for product name, price, and quantity with exact values
Create a format string variable for formatting output
Use sprintf to format the product details into a string
Use printf to print the formatted string
💡 Why This Matters
🌍 Real World
Formatting output is important when showing prices and quantities clearly on websites or reports.
💼 Career
Many jobs require formatting data for display or logging, and knowing <code>printf</code> and <code>sprintf</code> helps create professional outputs.
Progress0 / 4 steps
1
DATA SETUP: Create product variables
Create three variables: $productName with the value "Coffee Mug", $price with the value 12.5, and $quantity with the value 4.
PHP
Hint
Use = to assign values to variables. Strings need quotes.
2
CONFIGURATION: Create a format string
Create a variable called $format and set it to the string "Product: %s | Price: $%.2f | Quantity: %d\n". This string will be used to format the output.
PHP
Hint
The format string uses %s for strings, %.2f for floating numbers with 2 decimals, and %d for integers.
3
CORE LOGIC: Format the product details using sprintf
Create a variable called $formattedString and set it to the result of sprintf using $format, $productName, $price, and $quantity as arguments.
PHP
Hint
Use sprintf with the format string and variables in the correct order.
4
OUTPUT: Print the formatted string using printf
Use printf to print the variable $formattedString.
PHP
Hint
Use printf with the formatted string variable to display the output.
Practice
(1/5)
1. What does the %s format specifier do in printf or sprintf in PHP?
easy
A. It formats the variable as a string.
B. It formats the variable as an integer.
C. It formats the variable as a floating-point number.
D. It formats the variable as a boolean.
Solution
Step 1: Understand format specifiers
The %s specifier is used to insert a string value into the formatted output.
Step 2: Match specifier to variable type
Since %s is for strings, it converts the variable to a string representation.
Final Answer:
It formats the variable as a string. -> Option A
Quick Check:
%s = string [OK]
Hint: Remember %s means string, %d integer, %f float [OK]
Common Mistakes:
Confusing %s with %d or %f
Using %s for numbers without converting
Thinking %s formats booleans
2. Which of the following is the correct syntax to print a floating-point number with 2 decimal places using printf in PHP?
easy
A. printf("%.2f", $number);
B. printf("%2f.", $number);
C. printf("%f.2", $number);
D. printf("%f2.", $number);
Solution
Step 1: Identify correct decimal precision syntax
In PHP, to format a float with 2 decimals, use %.2f where .2 specifies 2 decimal places.
Step 2: Check each option's format string
Only printf("%.2f", $number); uses %.2f correctly; others have misplaced or incorrect syntax.
Final Answer:
printf("%.2f", $number); -> Option A
Quick Check:
%.2f = float with 2 decimals [OK]
Hint: Use %.Nf to print float with N decimals [OK]
Common Mistakes:
Placing decimal after %f instead of before
Using %2f instead of %.2f
Adding extra characters inside format string
3. What will be the output of the following PHP code?
%s inserts the string $name as is. %.1f formats $score as a float with 1 decimal place, rounding the value.
Step 2: Calculate rounded float
95.678 rounded to 1 decimal place is 95.7.
Final Answer:
Student: Alice, Score: 95.7 -> Option B
Quick Check:
%.1f rounds 95.678 to 95.7 [OK]
Hint: %.1f rounds float to 1 decimal place [OK]
Common Mistakes:
Not rounding float correctly
Confusing %s and %f usage
Expecting full float without formatting
4. Identify the error in this PHP code snippet:
$value = 123; printf("Value is %d.2", $value);
medium
A. The variable $value is not defined.
B. printf cannot print integers.
C. The format specifier %d.2 is invalid syntax.
D. The code is correct and prints 'Value is 123.2'.
Solution
Step 1: Check format specifier syntax
%d is for integers and does not support precision like .2. The correct usage is just %d.
Step 2: Understand error cause
Adding .2 after %d causes invalid format syntax, leading to an error or unexpected output.
Final Answer:
The format specifier %d.2 is invalid syntax. -> Option C
Quick Check:
%d does not take precision like %.2f [OK]
Hint: Use %d alone for integers; no decimal precision [OK]
Common Mistakes:
Trying to add decimal precision to %d
Confusing %d and %f format specifiers
Assuming printf auto-corrects invalid formats
5. You want to create a formatted string showing a product's name left-aligned in 10 spaces and its price right-aligned in 8 spaces with 2 decimals. Which sprintf format string is correct?