How can a for loop and the substr() function be used to separate the digits of a number in PHP?

To separate the digits of a number in PHP, you can use a for loop along with the substr() function. The for loop will iterate through each digit of the number, and the substr() function can be used to extract each digit individually. By converting the number to a string first, you can easily access each digit using substr().

$num = 12345;
$numStr = (string)$num;

for ($i = 0; $i < strlen($numStr); $i++) {
    $digit = substr($numStr, $i, 1);
    echo $digit . " ";
}