Here's a simple program which calculates, to eight decimal places, the value of the Golden Ratio φ implemented in both PHP and Perl, to demonstrate their similarities.
Perl
#!/usr/bin/perl
$a = 1;
$b = 1;
$c = undef;
$psi_old = undef;
print "Approximating psi...\n";
while (1)
{
$psi = sprintf('%.08f', $b/$a);
last if ($psi_old eq $psi);
$psi_old = $psi;
print "$psi\n";
$c = $a + $b;
$a = $b;
$b = $c;
}
PHP
<?php
$a = 1;
$b = 1;
$c = NULL;
$psi_old = NULL;
print "Approximating psi...\n";
while (1)
{
$psi = sprintf('%.08f', $b/$a);
if ($psi_old == $psi) break;
$psi_old = $psi;
print "$psi\n";
$c = $a + $b;
$a = $b;
$b = $c;
}
1?>