CS 334

Course Notes:


PHP Finally!

Chapter Two:

What is PHP?

  • PHP is a scripting language that can convert your web site from a STATIC document to an interactive application.

How do I create a PHP file?

  • Generally, PHP code commands can generally be embedded into an HTML page.
  • What makes PHP different is the embedded <script> elements.
  • When a user requests a PHP page, the server examines the page and executes any script elements before sending the resulting HTML to the user.
  • Three ways to indicate PHP in the code
    1. <? code goes here?> Easiest was but not always works.
    2. < ?php code goes here ?> This works better when the code is interpreted as XML. (How often will that happen with us?)
    3. Finally, with an HTML tag <script language="php">code goes here </script>
  • Save the file with a ".php" extension rather than the .htm or html extension.
  • Finally, needs to be saved on a web server that supports PHP.

Variables and Input.

Creating a string variable.

  • $variable _name="Hello"
  • The above line does two things.
    1. creates a variable called variable _name
    2. assigns the value "Hello" the the $variable _name
  • In PHP, all variables begin with a dollar sign. This distinguishes them from other program elements.

Naming a PHP variable.

  • Make the name descriptive
  • Use a manageable length
  • DON'T USE SPACES
  • Don't use symbols like #, *, /, \, ?
  • Case sensitive
  • Need to start with a letter, number or _

Printing a variable

<html>
<head>
<title>Hi Jacob</title>
</head>
<body>
<h1>Hi Jacob</h1>

<h3>Demonstrates using a variable</h3>

<?php

$userName = "Jacob";

print "Hi, $userName";

?>
</body>
</html>

Produces the output:

Hi Jacob

*Notice the semicolon. PHP is a more formal language than HTML and has some strict syntax rules such as putting a ; at the end of each code line instruction.

Creating a multi-line string. From here to here

$multi _line_variable= <<<HERE
Bla Bla Bla
Bla Bla Bla
HERE;
*Note: The ending "HERE" needs to be on a line by its self with no preceding spaces.

example:

<html>
<head>
<title>Row Your Boat</title>
</head>
<body>
<h1>Row Your Boat</h1>
<h3>Demonstrates use of long variables</h3>

<?

$verse = <<<HERE

Row, Row, Row, your boat<br>
Gently Down the stream<br>
Merrily, Merrily, Merrily, Merrily<br>
Life is but a dream!<br>
<br><br>
HERE;

print "<h3>Verse 1:</h3>";
print $verse;

print "<h3>Verse 2:</h3>";
print $verse;
print "<h3>Verse 3:</h3>";
print $verse;

?>

</body>
</html>

Numeric variables.

<?php

$x=10;
$y=5;

print $x + $y;

?>

produces an output 15

To quote or not to quote example:

<html>
<head>
<title>Three Plus Five</title>
</head>
<body>
<h1>Three Plus Five</h1>
<h3>Demonstrates use of numeric variables</h3>

<?
$x = 3;
$y = 5;

print "$x + $y = ";
print $x + $y;
print "<br><br>";

print "$x - $y = ";
print $x - $y;
print "<br><br>";

print "$x * $y = ";
print $x * $y;
print "<br><br>";

print "$x / $y = ";
print $x / $y;
print "<br><br>";

?>

</body>
</html>

* The mathematical operators will be the same as JavaScript.

Creating a form to ask a question

The what's your name? form.

Its common for PHP programs to be made of TWO or more seperate documents. An html page with a form and a PHP file for processing.

<html>
<head>
<title>What's your name?</title>
</head>
<body>
<h1>What's your name?</h1>
<h3>Writing a form for user input</h3>
<form method = "get" action = "hiUser.php">
Please type your name:
<input type = "text" name = "userName" value = "">
<br>
<input type = "submit">

</form>
</body>
</html>

This is the html file that captures the input and sends it for processing. This file sends the data to a file called "hiUser.php". Notice that in this case no path is specified so files must be in same directory.

The "hiUser.php" file.

<html>
<head>
<title>Hi User</title>
</head>
<body>
<h1>Hi User</h1>
<h3>PHP program that receives a value from "whatsName"</h3>

<?php

print "<h3>Hi there, $userName!</h3>";

?>

</body>
</html>

In the REAL WORLD the server you are working on may not have "register_globals in teh PHP.INI file turned ON. If that is the case use this in the PHP to retrieve the data.

$userName = $_REQUEST["userName"];

Reading Input from other form elements.

The "BORDER MAKER" example.

Again, we use TWO files. First the html file:

<html>
<head>
<title>Border Maker</title>
</head>
<body>
<center>
<h1>Border Maker</h1>
<h3>Demonstrates how to read HTML form elements</h3>

<form method = "post" action = "borderMaker.php">

<h3>Text to modify</h3>
<textarea name = "basicText" rows = "10" cols = "40">


Four score and seven years ago our fathers brought forth on this
continent a new nation, conceived in liberty and dedicated to the
proposition that all men are created equal. Now we are engaged in a
great civil war, testing whether that nation or any nation so
conceived and so dedicated can long endure.
</textarea>

<table border = 2>
<tr>
<td><h3>Border style</h3></td>
<td colspan = 2><h3>Border Size</h3></td>
</tr>

<tr>
<td>
<select name = borderStyle>
<option value = "ridge">ridge</option>
<option value = "groove">groove</option>
<option value = "double">double</option>
<option value = "inset">inset</option>
<option value = "outset">outset</option>
</select>
</td>
<td>

<select size = 5 name = borderSize>
<option value = "1">1</option>
<option value = "2">2</option>
<option value = "3">3</option>
<option value = "5">5</option>
<option value = "10">10</option>
</select>
</td>

<td>
<input type = "radio" name = "sizeType" value = "px">pixels<br>
<input type = "radio" name = "sizeType" value = "pt">points<br>
<input type = "radio" name = "sizeType" value = "cm">centimeters<br>
<input type = "radio" name = "sizeType" value = "in">inches<br>
</td>
</tr>
</table>

<input type = "submit" value = "show me">


</form>

</center>
</body>
</html>

Now the PHP file that processed the form data:

<html>
<head>
<title>Your Output</title>
</head>
<body>
<h1>Your Output</h1>
<center>
<?php
$theStyle = <<<HERE
"border-width:$borderSize$sizeType;
border-style:$borderStyle;
border-color:green"
HERE;

print "<div style = $theStyle>";
print $basicText;
print "</span>";

?>
</center>

</body>
</html>

Other examples:

Story Creater:

HTML FILE

<html>
<head>
<title>Story</title>
</head>
<body>
<h1>Story</h1>
<h3>Please fill in the blanks below, and I'll tell
you a story</h3>
<form method = "post" action = "story.php">

<table border = 1>
<tr>
<th>Color:</th>
<th>
<input type = "text" name = "color" value = "">
</th>
</tr>

<tr>
<th>Musical Instrument</th>
<th>
<input type = "text" name = "instrument" value = "">
</th>
</tr>

<tr>
<th>Animal</th>
<th>
<input type = "text" name = "anim1" value = "">
</th>
</tr>

<tr>
<th>Another animal</th>
<th>
<input type = "text" name = "anim2" value = "">
</th>
</tr>

<tr>
<th>Yet another animal!</th>
<th>
<input type = "text" name = "anim3" value = "">
</th>
</tr>

<tr>
<th>Place</th>
<th>
<input type = "text" name = "place" value = "">
</th>
</tr>

<tr>
<th>Vegetable</th>
<th>
<input type = "text" name = "vegetable" value = "">
</th>
</tr>

<tr>
<th>A structure</th>
<th>
<input type = "text" name = "structure" value = "">
</th>
</tr>

<tr>
<th>An action</th>
<th>
<select name = "action">
<option value = "fast asleep">fast asleep</option>
<option value = "drinking cappucino">drinking cappucino</option>
<option value = "wandering around aimlessly">wandering around aimlessly</option>
<option value = "doing nothing in particular">doing nothing in particular</option>
</select>
</th>
</tr>

<tr>
<td colspan = 2> <center>
<input type = "submit" value = "tell me the story">
</center>
</td>
</tr>
</table>

</form>
</body>
</html>

PHP FILE

<html>
<head>
<title>Little Boy Who?</title>
</head>
<body>
<center>

<h1>Little Boy Who?</h1>

<?php

print <<<HERE
<h3>
Little Boy $color, come blow your $instrument!<br>
The $anim1's in the $place, the $anim2's in the $vegetable.<br>
Where's the boy that looks after the $anim3?<br>
He's under the $structure, $action.
</h3>
HERE;
?>

</center>

</body>
</html>