Bird
Raised Fist0
PHPprogramming~10 mins

Printf and sprintf formatting in PHP - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to print the number 10 using printf.

PHP
<?php
$num = 10;
printf("%[1]d", $num);
?>
Drag options to blanks, or click blank then click option'
Ad
Bs
Cf
Dx
Attempts:
3 left
💡 Hint
Common Mistakes
Using %s which is for strings.
Using %f which is for floating-point numbers.
2fill in blank
medium

Complete the code to format a float number with 2 decimal places using sprintf.

PHP
<?php
$price = 12.3456;
$formatted = sprintf("%.[1]f", $price);
echo $formatted;
?>
Drag options to blanks, or click blank then click option'
A1
B3
C2
D4
Attempts:
3 left
💡 Hint
Common Mistakes
Using %.1f which shows only one decimal place.
Using %.3f which shows three decimal places.
3fill in blank
hard

Fix the error in the code to print a padded number with leading zeros using printf.

PHP
<?php
$num = 7;
printf("%[1]d", $num);
?>
Drag options to blanks, or click blank then click option'
A0
B07
Cd
D7
Attempts:
3 left
💡 Hint
Common Mistakes
Using %7d pads with spaces, not zeros.
Using %0d is invalid format.
4fill in blank
hard

Fill both blanks to format a string with a minimum width and left alignment using printf.

PHP
<?php
$name = "Bob";
printf("%[1][2]s", $name);
?>
Drag options to blanks, or click blank then click option'
A-
B10
C5
D+
Attempts:
3 left
💡 Hint
Common Mistakes
Omitting '-' causes right alignment.
Using '+' is invalid for string alignment.
5fill in blank
hard

Fill both blanks to format an integer in hexadecimal with uppercase letters and width 6 padded with zeros using sprintf.

PHP
<?php
$num = 255;
$hex = sprintf("%[1][2]X", $num);
echo $hex;
?>
Drag options to blanks, or click blank then click option'
A0
B6
C-
D+
Attempts:
3 left
💡 Hint
Common Mistakes
Using '-' pads with spaces and left aligns.
Omitting '0' pads with spaces instead of zeros.

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

  1. Step 1: Understand format specifiers

    The %s specifier is used to insert a string value into the formatted output.
  2. Step 2: Match specifier to variable type

    Since %s is for strings, it converts the variable to a string representation.
  3. Final Answer:

    It formats the variable as a string. -> Option A
  4. 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

  1. Step 1: Identify correct decimal precision syntax

    In PHP, to format a float with 2 decimals, use %.2f where .2 specifies 2 decimal places.
  2. Step 2: Check each option's format string

    Only printf("%.2f", $number); uses %.2f correctly; others have misplaced or incorrect syntax.
  3. Final Answer:

    printf("%.2f", $number); -> Option A
  4. 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?
$name = "Alice";
$score = 95.678;
printf("Student: %s, Score: %.1f", $name, $score);
medium
A. Student: Alice, Score: 95.6
B. Student: Alice, Score: 95.7
C. Student: Alice, Score: 95.68
D. Student: Alice, Score: 95.678

Solution

  1. Step 1: Analyze format specifiers

    %s inserts the string $name as is. %.1f formats $score as a float with 1 decimal place, rounding the value.
  2. Step 2: Calculate rounded float

    95.678 rounded to 1 decimal place is 95.7.
  3. Final Answer:

    Student: Alice, Score: 95.7 -> Option B
  4. 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

  1. Step 1: Check format specifier syntax

    %d is for integers and does not support precision like .2. The correct usage is just %d.
  2. Step 2: Understand error cause

    Adding .2 after %d causes invalid format syntax, leading to an error or unexpected output.
  3. Final Answer:

    The format specifier %d.2 is invalid syntax. -> Option C
  4. 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?
$product = "Pen";
$price = 1.5;
$result = sprintf("...", $product, $price);
hard
A. "%-8s%-10.2f"
B. "%10s%-8.2f"
C. "%10s%8.2f"
D. "%-10s%8.2f"

Solution

  1. Step 1: Understand alignment and width specifiers

    %-10s means left-align string in 10 spaces. %8.2f means right-align float in 8 spaces with 2 decimals.
  2. Step 2: Match requirements to options

    "%-10s%8.2f" matches left-aligned product name in 10 spaces and right-aligned price with 2 decimals in 8 spaces.
  3. Final Answer:

    "%-10s%8.2f" -> Option D
  4. Quick Check:

    %-10s left-align string, %8.2f right-align float [OK]
Hint: Use - for left-align, number for width, .N for decimals [OK]
Common Mistakes:
  • Mixing alignment flags (-) with wrong specifier
  • Swapping width numbers for string and float
  • Forgetting .2 for decimal precision on float
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard