How can I enforce 2 digits in an integer in PHP?

To enforce 2 digits in an integer in PHP, you can use the str_pad function to add leading zeros to the integer if it is less than 10. This will ensure that the integer always has 2 digits.

$num = 5;
$num = str_pad($num, 2, '0', STR_PAD_LEFT);
echo $num; // Output: 05