Information in this document came from PHP Manual
and the Introductory Tutorial.
What is Server Side Scripting?
A tool that lets you create dynamic web pages. Sripting languages such as PHP, associated with the
Apache Web Server and VB Script, associated with Microsoft's Internet Information Server allow the
mixing of HTML and the scripting language in the same file. This methodology differs from compiled CGI programs.
You can write executables in C, C++, Visual Basic that can create Dynamic Web pages, but the HTML has to be
embedded in the code. Web pages created with scripting languages are treated just like regular HTML pages,
you can create and edit them the same way you normally create regular HTML pages.
Why study PHP?
Since we have access to an Apache Web Server with the PHP Parser, we will examine the use of PHP to
create Dynamic Web Pages. PHP (recursive acronym for "PHP: Hypertext
Preprocessor") is a widely-used Open Source general-purpose scripting language
that is especially suited for Web development and can be embedded into HTML.
<html>
<head>
<title>Hello World</title>
</head>
<body>
<?php
echo "Hello World";
?>
</body>
</html>
|
The PHP code is enclosed in special start <?php and end ?>
tags that allow you to jump into and out of "PHP mode".
Escaping from HTML
When PHP parses a file, it simply passes the text of the file through until
it encounters one of the special tags which tell it to start interpreting the
text as PHP code. The parser then executes all the code it finds, up until it
runs into a PHP closing tag, which tells the parser to just start passing the
text through again. This is the mechanism which allows you to embed PHP code
inside HTML: everything outside the PHP tags is left utterly alone, while
everything inside is parsed as code.
Enought to get started
Instruction Separation
Instructions are separated by the ;
Comments
Supports C, C++ and Unix Shell type comments
<?php echo "A comment follows"; // One-line c++ style comment
/* This is a multi line
comment multiline comments span more than one line*/
echo "Line 1";
// # can be used to place comments at end of line
echo "Line 2"; # This is shell-style style comment
?>
|
Data Types
Supports the following
- Boolean
- Integer
- Floating Point
- String
- Array
- Object
Varaiables
Rules for Variables Name
- Variables begin with a $
- Case sensitive
- starts with a letter or underscore
- followed by a number, leter, or underscore
<?php
// can use single quotes for string and use ' in string by escaping \'
$str = Stan\'s test';
echo $str;
// can use double quotes for strings
echo "<br>";
// can use "" quotes for string and use " in string by escaping \"
$str1 = "Stan said: \"try this\"";
echo $str1;
echo "<br>";
$test_1 = 3.45;
echo $test_1;
$_intcntr = 3;
echo $_intcntr;
// can initialize array element by giving array name and inder in square brackets
$ar[0] = "Stan";
$ar[1] = "Mary";
$ar[2] = "Kathy";
// can print an array one element element at a time
for($i=0;$i<3;$i++)
{
echo $ar[$i];
echo "<br>";
}
?>
|
Processing the Data Returned from Form
PHP can access
the values returned in the form's fields. PHP creates a variable with same same
name as the input tag's name. In the form below, the value entered into the text
box with name "first" will be contained in a variable called $first, the text box
called "last" will have its value returned in a variable called $last. The echo
function can be used to print the value contained in the variable holding
the value returned by the form.
Sample Form
<html>
<body>
<form method="post" action="process.php" id=form1 name=form1>
First name:<input type="Text" name="first"><br>
Last name:<input type="Text" name="last"><br>
Address:<input type="Text" name="address"><br>
Position:<input type="Text" name="position"><br>
<input type="Submit" name="submit" value="Enter information">
</form>
</body>
</html>
|
PHP file to process above form
<html>
<body>
<?php
echo "first=$first<br>";
echo "last=$last<br>";
echo "addresst=$address<br>";
echo "position=$position<br>";
?>
</body>
</html>
|
PHP file to process any form
HTTP_POST_VARS is an array of all the fieldname, value pairs returned by the form and exposed by PHP.
The each function returns the current key and value pair from the array $HTTP_POST_VARS and
advances the array cursor. If the internal pointer for the array points past the end of the array contents,
each() returns FALSE. List() is a language construct that is used to assign a list of variables in
one operation. In this case, the fieldname, value pair returned by the each() function is assigned to
$name and $value .
<html>
<body>
<?php
while (list($name, $value) = each($HTTP_POST_VARS)) {
echo "$name = $value<br>\n";
}
?>
</body>
</html>
|