0
0
PHPprogramming~10 mins

Printf and sprintf formatting in PHP - Interactive Code Practice

Choose your learning style9 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.