Print All Digits of a Number using Recursion
You can also use recursion to print digits of a number. The base case is when the number becomes 0. In this case, we repeatedly divide number and print the remainder.
<?php
function printDigits($number) {
if ($number > 0) {
printDigits((int)($number / 10));
echo $number % 10 . " ";
}
}
// Driver Code
$num = 12345;
printDigits($num);
?>
Output
1 2 3 4 5
PHP Program to Print All Digits of a Given Number
Given a number, the task is to print all digits of the given number in PHP. There are various methods to print all digits of the given number, we will cover each approach with explanations and examples.
Table of Content
- Using String Conversion
- Using Modulus Operator (%)
- Using Recursion
- Using Array and Loop