Larger Text
Normal Text
Smaller Text
IT 280 Web Development II, Spring 2008
Dynamic pages with PHP - part 1
- PHP is a server-parsed language. It is processd by the http server program before the page is returned to the browser.
- The name "PHP" is a recursive acronym that stands for "PHP: Hypertext Preprocessor".
- PHP is available on many platforms (Linux or Unix with Apache, Windows with Apache or IIS).
- PHP is a complete programming language that has many powerful functions.
- Some of the functions and language features are especially designed for the Web environment.
PHP Blocks
- PHP statements are incorporated into an HTML document using special tags and are interpreted by the server before the document is returned to the browser.
- PHP lets you put many statements together inside a single tag called a block.
- Each statement within a block must end with a semicolon.
- A PHP block in a document starts with .
- A PHP block can be placed anywhere in the document.
- When the document is requested, the program statements in the block are executed and the entire block is replaced with the result.
Comments
- Single-line comments in PHP begin with
//.
- Everything from the // to the end of the line is comment.
- You can also use multi-line comments delimited by /* and */.
- As with all programming, you should include appropriate comments in your code.
Example:
<?php
//This is a comment. It is for the programmer to read.
/*This is another comment.
It could provide a longer explanation
of the program for the reader
This form of comment is also useful if
you text editor tends to break long lines
without warning you
*/
?>
The echo command
- The
echo command inserts output into the returned document.
- It is similar to rthe JavaScript document.write
- Its arguments can be expressions using strings, numbers, operators, and variables.
- Multiple arguments are separated by commas.
- You can have line breaks inside a quoted string literal.
Example:
<?php
echo "Hello there!"; // A basic statement
echo "2 + 3 = ", 2 + 3; // Two arguments separated by a comma.
echo "This quoted source text
spreads across
more than one line.";
?>
Publishing PHP documents
- PHP documents must be stored in the web documents directory (your WWW directory)
- File extension: PHP documents must have the extension
.php.
- If the file does not have the correct extension, the PHP statements will not be interpreted.
Example 1:
<html>
<head>
<title>PHP Example One</title>
</head>
<body>
<h1>This is the first PHP example</h1>
<?php
echo "Hello there!"; // A basic statement
echo "2 + 3 = ", 2 + 3; // Two arguments separated by a comma.
echo "This source text
spreads across
more than one line.";
?>
</body>
</html>
The example directory for the first example contains the example as
- first.txt: plain text - you see all the code as written.
- first.htm: HTML - the PHP commands are treated as comments
- first.php: server-parsed file - the PHP commands are executed and you get only the results.
Variables
- Variables in PHP are written with a dollar sign followed by the name of the variable.
- Variable names are case-sensitive.
- A variable name must begin with a letter or underscore, followed by any number of letters, numbers, or underscores.
- Variables are assigned values using the
= operator.
Example:
<?php
$aa = "Hello ";
$bb = "world! ";
echo $aa, $bb;
$xx = 12;
$yy = 10;
echo $xx * $yy;
?>
The example directory for the second example contains the example as
- second.txt: plain text - you see all the code as written.
- second.php: server-parsed file - the PHP commands are executed and you get only the results.
Predefined variables
- PHP provides a large number of predefined variables.
- Many of these variables depend upon which server is running, the version and setup of the server, and other factors.
- Some of the predefined variables provide access to the values that are shown by the
phpinfo() function discussed last week.
- Their names are indicated in the table headed PHP Variables near the end of the page.
- Most of the names begin with an underscore. It is part of the name.
- Many variables have the same basic name followed by a string in square brackets. The brackets and string are part of the name.
- Warning: the
phpinfo() output only shows the name part; You must prepend the $. -
Example:
<h1>This is the third PHP example</h1>
<?php echo $_SERVER["QUERY_STRING"]; ?>
<!-- This part is in the HTML, not inside a PHP block -->
<!-- So these are HTML comment elements. -->
<br />
<!-- break tag for a more readable page. -->
<?php
// We can print the break from inside a PHP block
// In here we must use PHP Comments
echo $_SERVER["REMOTE_ADDR"], "<br />";
?>
<?php
echo $_SERVER["REQUEST_URI"], "<br />";
echo $_SERVER["PATH_TRANSLATED"];
?>
The example directory for the third example contains the example as
- third.txt: plain text - you see all the code as written.
- third.php: server-parsed file - the PHP commands are executed and you get only the results.
Activity
- First request the third.php example
.
- Then edit the URL in the browser's location bar so it requests
third.php?Hello_World!.
The include function
The PHP function include
- should have the name of the included file following the command
- the file name is a string
- it can be a literal (in quotes) or a variable
- tells the server to:
- get the specified file
- process it like another .php file (no matter what its extension)
- insert the results into the final document.
The name of the included file is written following the command.
Example:
>
<html>
<!-- The file fourth.php -->
<head><title>fourth PHP page</title</head>
<body>
<h1>Fourth PHP Example - using include</h1>
<?php include "included.txt"; ?>
<p>Notice that even though the included file has the extension .txt, the PHP commands in it are processed.</p>
</body>
</html>
<!-- The file included.txt -->
<p>The following line comes from the included file.</p>
<h2><?php
echo 2 + 3 * 5 ?></h2>
The example directory for the fourth example contains
- the example as fourth.txt: plain text - you see all the code as written.
- the example as fourth.php: server-parsed file - the PHP commands are executed and you get only the results.
- the included.txt: the included file.
The PHP date function
- The PHP
date() function will return the current date and time in a variety of formats.
- Its argument should be a string that includes codes for the formats.
A few of the many possible codes are
d Day of the month as 2 digits with leading zeros 01 to 31
D Day of the week as three letters Mon through Sun
l Full name of the day of the week (lowercase 'L') Sunday through Saturday
M Name of the month as three letters Jan through Dec
F Full name of the month January through December
Y Numeric representation of a year as 4 digits Examples: 1999 or 2005
- There can be other characters in the string. If they are not codes, they will be unchanged.
- If you want to include a character literally but it is a code, you must precede it with a backslash.
- For more information about the function, including a complete list of
codes, see the documentation on
it.
Example:
>
<?php
echo "Today is ";
echo date ("l, F d, Y");
?>
In this example, we did not place the "Today is" inside the date argument string because the phrase contains letters such as d and y that would have been interpreted as codes.
Alternatively, we could have escaped the characters. However there are so many codes that we would have to escape every letter!
<?php
echo date ("\T\o\d\a\y \i\s l, F d, Y");
?>
The example directory for the fifth example contains
- the example as fifth.txt: plain text - you see all the code as written.
- the example as fifth.php: server-parsed file - the PHP commands are executed and you get only the results.
- the fifth_x.php: a version with the characters "Today is" inside the argument string.
- the fifth_z.php: a version with the escaped characters "\T\o\d\a\y \i\s" inside the argument string.
Conditional statements
- PHP has a conditional
if..elseif..else statement.
- The condition must be enclosed in parentheses.
- The basic operators are the same as in JavaScript and several other languages. In particular, == tests for equality.
Example:
>
<?php
$x = date("D");
if ($x == "Fri")
{
echo "It's Friday!!!! ";
echo "Have a great weekend!";
}
else
{
echo "Have a nice day.";
echo "<br />Try me again on Friday!";
}
?>
The example directory for the sixth example contains
- the example as sixth.txt: plain text - you see all the code as written.
- the example as sixth.php: server-parsed file - the PHP commands are executed and you get only the results.
Exercise Lab 09
For this exercise, you will create a basic PHP document. It should:
- Include your name in PHP comments.
- Display (echo) the text "This is a PHP page." The line should be a level 1 heading.
- Assign the values 13 and 89 to variables $alphy and $bety
- Display the sum and product f the variables, with appropriate text.
- include a file named morestuf.txt. The included file should have PHP commands to display a list with
- the identity (PHP_SELF) of the document that is being
executed.
- the date in the format YYYY/MM/DD (4-digit year, 2-digit month and day)
- the request's QUERY_STRING, HTTP_USER_AGENT, REMOTE_ADDR, and
REQUEST_METHOD
Be sure to add the lab to your table of contents page and to complete the
entry on Blackboard.