PHP programming tutorials
Run your first PHP script, work through sorting examples, and look up math functions, ZIP archive operations, and error reporting.
PHP core curriculum
Getting started
Decisions and loops
Functions and data
Files and projects
Reverse the digits of a number
<?php
class ReverseNumber {
public function reverse() {
$input = 123;
$output = 0;
$temp = $input;
while ($temp > 0) {
$remainder = $temp % 10;
$output = ($output * 10) + $remainder;
$temp = (int)($temp / 10);
}
echo("Original Number: " . $input . "\n");
echo("Reverse Number: " . $output . "\n");
}
}
$obj = new ReverseNumber();
$obj->reverse();
Original Number: 123 Reverse Number: 321