PHP Code Examples
[Download]   [Execute]
<?php $title = 'Conditionals I'; include '../head1.inc'; 
// Note: 
//   1. The explode function returns an array.
//   2. The list notation on the left provides a list assignment of the
//      values from explode.
list($i1, $i2, $i3, $i4) = explode('.', $_SERVER["REMOTE_ADDR"]);

echo <<<END
You are browsing from $i1.$i2.$i3.$i4.<p>
Since IP numbers are so confusing, we can use a graphical
method to make this clearer.<p>
Here is a histogram of your IP number:<p><tt>

print "&nbsp;&nbsp;&nbsp; ";
$i = floor($i1/4);
while($i--) echo "x";
echo "<br>";

print "&nbsp;&nbsp;&nbsp; ";
$i2 /= 4;
for($i = 1; $i <= $i2; ++$i) echo 'x';
echo "<br>";

print "&nbsp;&nbsp;&nbsp; ";
$i3 /= 4;
if($i3 >= 1) {
	do {
		echo 'x';
	while($i3 > 0);
echo "<br>";

// If's and loops have an alternative syntax, using a colon after the
// loop header and an end keyword.  No brackets are required.
print "&nbsp;&nbsp;&nbsp; ";
for($i = $i4; $i > 0; $i -= 4): 
	echo 'x';
print "</tt><p>Isn't that better?<p>";

// Plain HTML sections can be treated essentially as print statements,
// and controlled by ifs.
if($i1 <= $i4): ?>
The first byte of your IP number is less than or equal
to the last byte.  This means the front of your computer
is smaller than the back.  You should check for proper support.
<?php else: ?>
The first byte of your IP is larger than the last byte.
This means that your garfrabulator is larger than your
muphoid cyberlink.  This means that your computer is very
economical to operate, as it produces more electricity than
it consumes.
<?php endif ?>