PHP vs Perl

This is a very old article. It has been imported from older blogging software, and the formatting, images, etc may have been lost. Some links may be broken. Some of the information may no longer be correct. Opinions expressed in this article may no longer be held.

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?>