PHP
PHP is another server technology that lets programmers blend HTML and programming language code. In this regard, it is like ASP and JSP.
Unlike ASP, it is a platform-independent product that is available as a free download. Check out http://www.php.net/downloads.php.
You will have an account on our Linux server where you can do a homework assignment and run the sample examples.
The scripting language itself is most like Perl.
A couple of good links on PHP are http://www.php.net/manual/en/tutorial.php, http://www.zend.com/manual/, and http://www.phpworld.com/faq/faq_000.html .
I will have two sets of links in this document. Use the internal links if you are on campus, and use the external links if you are off campus.
First example: ex1.php External Internal
<html>
<head>
<title>PHP Test</title>
</head>
<body>
<?php
echo "<p>Hello World</p>"; ?>
</body>
</html>
Second example: ex2.php External Internal
Note what variables are available.
<html>
<head>
<title>Pre-defined
Variables</title>
</head>
<body>
<?php
phpinfo(); ?>
</body>
</html>
Third example ex3.php External Internal
Checking for browser.
<html>
<head>
<title>What
Browser?</title>
</head>
<body>
Value of HTTP_USER_AGENT:
<br>
<?php
echo $_SERVER["HTTP_USER_AGENT"]; ?>
<p align="center">
<?php
if (strstr($_SERVER["HTTP_USER_AGENT"],
"MSIE"))
{
?>
<b>You are using Internet
Explorer</b>
<?php
}
else
{
?>
<b>You are not using Internet Explorer</b>
<?php
}
?>
</p>
</body>
</html>
Fourth example ex4.htm External Internal
Using form data.
<html>
<body>
<form
method="POST"
action="ex4.php">
<H2>Choose
your options</H2>
<select
name="favoriteBird">
<option value="flamingo">
Flamingo
<option value="robin"> Robin
<option value="cardinal">
Cardinal
<option value="eagle"> Eagle
<option value="penguin">
Penguin
<option value="bluejay">
Blue Jay
</select>
<BR>
User Name: <input type=text name="userName">
<BR>
Color: <input type=text name="favoriteColor">
<BR>
<input type=submit value="Send Data">
</form>
</body>
</html>
ex4.php
<HTML>
<HEAD>
<TITLE>Color and Bird?</TITLE>
</HEAD
<BODY>
<?php
$username = $_POST["name"];
$color = $_POST["favoriteColor"];
$bird = $_POST["favoriteBird"];
?>
<FONT
COLOR='<?php echo $color;
?>'>
Hello,
<?php
echo $color; ?>
.
</FONT>
<BR>
Your favorite bird is the
<?php
echo $bird; ?>
.
Here
is a picture!
<P>
<IMG SRC="<?php
echo $bird; ?>.jpg">
</BODY>
</HTML>
Fifth example: ex5.htm External Internal
<html>
<body>
<form
method="POST"
action="ex5.php">
<h3>Choose
your operation</h3>
<input
type="radio" name="operator" value="+"> +
<br><input type="radio"
name="operator" value="-"> -
<br><input type="radio"
name="operator" value="*"> *
<br><input type="radio"
name="operator" value="/"> /
<br><input type="radio"
name="operator" value="^"> ^
<h3>Choose
your operands </h3>
<input
type="text" name="operand1">
<input
type="text" name="operand2">
<h3>Want
a bird?
<input
type="checkbox" name="bird" value="yes">
</h3>
<h3>Choose
a city
<select
name="city">
<option value="buffalo">
<option value="
<option value="
<option value="
</select>
</h3>
<p>
<input type=submit value="Send Data">
</form>
</body>
</html>
ex5.php
<HTML>
<HEAD>
<TITLE>Arithmatic</TITLE>
</HEAD
<BODY>
<h1>
Answer
is
<?php
// returns a to the b power (assuming b is an
integer
function power($a,
$b)
{
$answer = 1;
for ($i = 1; $i <= $b; $i++)
{
$answer = $answer * $a;
}
return $answer;
}
function make_seed()
{
// explode splits the second argument using
first argument as
// delimiter
// list function used to assign values to
multiple variables
// microtime
returns the string "msec sec" (time since
list($usec, $sec) = explode(' ', microtime());
return (float) $sec + ((float) $usec * 100000);
}
function randomBird()
{
srand(make_seed());
$birds = array(1
=> "robin.jpg", "penguin.jpg", "cardinal.jpg",
"flamingo.jpg",
"eagle.jpg", "bluejay.jpg");
$choice = rand(1,6);
return
$birds[$choice];
}
$distances = array("
"
switch
($_POST["operator"])
{
case "+":
echo
$_POST["operand1"] + $_POST["operand2"];
break;
case "-":
echo
$_POST["operand1"] - $_POST["operand2"];
break;
case "*":
echo
$_POST["operand1"] * $_POST["operand2"];
break;
case "/":
echo
$_POST["operand1"] / $_POST["operand2"];
break;
case "^":
echo power($_POST["operand1"],
$_POST["operand2"]);
// could actually use the pow function instead
break;
}
?>
.
</h1>
<?php
if
($_POST["bird"] != null)
{
echo "<img src='" . randomBird()
. "'>";
}
echo
"<P>Distance to " . $_POST["city"]
. " is " .
$distances[$_POST["city"]];
?>
</BODY>
</HTML>
Sixth example ex6.htm External Internal
<html>
<body>
<form
method="POST"
action="ex6.php">
<h3>What's
your team?</h3>
<select
name="team">
<option
value="Yankees">Yankees
<option value="Red Sox">Red
Sox
<option
value="Orioles">Orioles
<option value="Devil
Rays">Devil Rays
<option value="Blue
Jays">Blue Jays
<option value="Royals">Royals
<option value="White
Sox">White Sox
<option value="Twins">Twins
<option
value="Indians">Indians
<option value="Tigers">Tigers
<option
value="Mariners">Mariners
<option value="As">A's
<option value="Angels">Angels
<option
value="Rangers">Rangers
</select>
<p>
<input type=submit value="Send Data">
</form>
</body>
</html>
ex6.php
<HTML>
<HEAD>
<TITLE>Team Record</TITLE>
</HEAD
<BODY>
<h1>
<?php
$myteam=$_POST["team"];
$fp = fopen("teams.txt",
"r");
$i = 0;
while ($line = fgets($fp, 4096)) // read line
from file, including \n
{
list($team, $win,
$loss) = explode(",", $line);
$wins[$team] =
$win;
$losses[$team] = rtrim($loss); // removes whitespace
from end
}
fclose($fp);
echo $myteam . ": " . $wins[$myteam] . "-" . $losses[$myteam];
?>
</h1>
</BODY>
</HTML>
Seventh example ex7.htm External Internal
<html>
<body>
<form
method="POST"
action="ex7.php">
<h3>Enter
a score: </h3>
<select
name="team1">
<option
value="Yankees">Yankees
<option value="Red Sox">Red
Sox
<option
value="Orioles">Orioles
<option value="Devil
Rays">Devil Rays
<option value="Blue
Jays">Blue Jays
<option value="Royals">Royals
<option value="White
Sox">White Sox
<option value="Twins">Twins
<option
value="Indians">Indians
<option value="Tigers">Tigers
<option
value="Mariners">Mariners
<option value="As">A's
<option value="Angels">Angels
<option
value="Rangers">Rangers
</select>
<input
type="text" name="score1">
<select
name="team2">
<option
value="Yankees">Yankees
<option value="Red Sox">Red
Sox
<option
value="Orioles">Orioles
<option value="Devil
Rays">Devil Rays
<option value="Blue
Jays">Blue Jays
<option value="Royals">Royals
<option value="White
Sox">White Sox
<option value="Twins">Twins
<option
value="Indians">Indians
<option value="Tigers">Tigers
<option value="Mariners">Mariners
<option value="As">A's
<option value="Angels">Angels
<option
value="Rangers">Rangers
</select>
<input
type="text" name="score2">
<p>
<input type=submit value="Send Data">
</form>
</body>
</html>
ex7.php
<HTML>
<HEAD>
<TITLE>Team Record</TITLE>
</HEAD
<BODY>
<?php
$team1=$_POST["team1"];
$team2=$_POST["team2"];
$score1=$_POST["score1"];
$score2=$_POST["score2"];
if ($score1 == $score2)
echo "Ties not possible";
else if ($team1 == $team2)
echo "Need two different teams";
else if ($score1 > $score2)
{
$winner = $team1;
$loser = $team2;
}
else
{
$winner = $team2;
$loser = $team1;
}
$lines = file("teams.txt");
// read file into an array
$fp = fopen("teams.txt",
"w"); // permission must be set for write
for ($i = 0; $i < sizeof($lines) ; $i++)
{
list($thisteam, $wins, $losses) = explode(",", rtrim($lines[$i]));
if ($thisteam ==
$winner)
$wins++;
if ($thisteam ==
$loser)
$losses++;
fwrite($fp, $thisteam
. "," . $wins . "," . $losses . "\n");
}
fclose($fp);
$lines = file("teams.txt");
for ($i = 0; $i < sizeof($lines) ; $i++)
{
$data = explode(",",
rtrim($lines[$i]));
$data[3] =
$data[1] / ($data[1] + $data[2]);
$data = array_reverse($data);
$lines[$i] = implode(",", $data);
}
rsort($lines);
?>
<h1>
New Standings </h1>
<table>
<tr> <th>Team</th> <th>Wins</th><th>Losses</th></tr>
<?php
for ($i = 0; $i < sizeof($lines) ; $i++)
{
$data = explode(",",
$lines[$i]);
echo "<tr><td>" . $data[3]
. "</td><td>" . $data[2]
. "</td><td>" . $data[1] .
"</td></tr>";
}
?>
</h1>
</BODY>
</HTML>
Eighth example ex8.php External Internal
<html>
<body>
<form
method="POST"
action="ex9.php">
<h3>Enter
a score: </h3>
<select
name="team1">
<?php
$teams=file("teamlist.txt");
foreach ($teams as
$team)
{
echo
"<option value=\"" . rtrim($team) . "\">" . rtrim($team) .
"\n";
}
?>
</select>
<input
type="text" name="score1">
<select
name="team2">
<?php
$teams=file("teamlist.txt");
foreach ($teams as
$team)
{
echo
"<option value=\"" . rtrim($team) . "\">" . rtrim($team) .
"\n";
}
?>
</select>
<input
type="text" name="score2">
<p>
<input type=submit value="Send Data">
</form>
</body>
</html>
ex9.php
<HTML>
<HEAD>
<TITLE>Team Record</TITLE>
</HEAD
<BODY>
<?php
$score[0]=$_POST["team1"];
$score[1]=$_POST["score1"];
$score[2]=$_POST["team2"];
$score[3]=$_POST["score2"];
$fp = fopen("scores.txt",
"a+");
// permission must be set for append,
create if nec
fputs($fp, implode(",",
$score) . "\n");
?>
Click
<a href="scores.txt"> here for all
scores.
</BODY>
</HTML>
Ninth Example ex10.htm External Internal
<html>
<body>
<h1>
Add a Team </h1>
<form
method="POST"
action="ex10.php">
<h3>Enter
a new team: </h3>
<input
type="text" name="team">
<p>
<input type=submit value="Send Data">
</form>
</body>
</html>
ex10.php
<HTML>
<HEAD>
<TITLE>Team Record</TITLE>
</HEAD
<BODY>
<?php
$team=$_POST["team"];
$fp = fopen("teamlist.txt",
"a"); // permission must be set for write
fputs($fp, $team . "\n");
echo $team . " is added";
?>
</BODY>
</HTML>
ex10.php
<HTML>
<HEAD>
<TITLE>Team Record</TITLE>
</HEAD
<BODY>
<?php
$team=$_POST["team"];
$fp = fopen("teamlist.txt",
"a"); // permission must be set for write
fputs($fp, $team . "\n");
echo $team . " is added";
?>
</BODY>
</HTML>