Larger Text Normal Text Smaller Text

IT 280 Web Development II, Spring 2008

 

Dynamic pages with PHP - part 1

PHP Blocks

Comments

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

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

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

Variables

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

Predefined variables

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

Activity

  1. First request the third.php example
  2. .
  3. Then edit the URL in the browser's location bar so it requests third.php?Hello_World!.

The include function

The PHP function include

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 PHP date function

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

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

Conditional statements

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

Exercise Lab 09

For this exercise, you will create a basic PHP document. It should:

Be sure to add the lab to your table of contents page and to complete the entry on Blackboard.

Comments to: chang@hood.edu
Last Modified: 11 May 2008. 20:24
Valid CSS! Valid XHTML 1.0!

 

Hood College Department of Computer Science: Course materials © 1997-2006 by Elizabeth Chang.