Functions I
PHP Code Examples
[Download]   [Execute]
<?php $title = 'Functions I'; include '../'; 
include '../';

// This code again generates a histogram of the viewer's IP address.
// This one is prettier, as it uses image tags to build bars, and uses
// a table to arrange them.  We use a function which is called to generate
// each bar.

// Function to generate a bar for the IP histogram using tables.  Each 
function bar ($label, $length) {
	$maxbar = 400;		// Max bar width in pixels.
	$maxlen = 255;		// Max value of an IP component.
	$barlen = round($maxbar * ($length/$maxlen));

	// Generate a table row with some left skip, the number $length,
	// and a bar proportional to $length.
	echo <<<ENDROW

	   <td width=10></td>
	   <td align="right">$label:</td>
	   <td align="left"><img src="/~bennet/icons/black_pixel.gif"
		         height=20 width=$barlen></td>

$remote = $_SERVER["REMOTE_ADDR"];
echo <<<END
You are browsing from $remote.<p>
Since some of you did not seem greatly enlightened
by our last histogram, we have provided a prettier one.

// This generates the table, and takes a total of the parts.  The foreach
// explodes the address into parts, then goes through the parts.
echo "<table>\n";
$tot = 0;
foreach (explode('.', $remote) as $comp) {
	bar($comp, $comp);
	$tot += $comp;

// Compute the average, then generate a bar for it, after creating some
// blank space.
$av = $tot/4;
echo "<tr><td>&nbsp;</td></tr>\n";
bar("Average ($av)", $av);

echo "</table>\n";