Metropolitan State University

ICS 325

Building Services on the Internet

 

Lab 5

PHP's Basic Syntax

(Linux Redhat 7.1)

 

1.         Hello World using the print statement.

§         Log onto "redhat.ics.metrostate.edu" using your class account, then do the following:

$ cd

$ cd public_html

$ mkdir lab5

$ cd lab5

§         Edit an HTML document called "hello1.php" using pico with the following lines:

 

<?php

  /*****************************************

    Author: Your Name

    Date:   Current Date

    Description:

      This PHP script contains my first PHP

      script.  The Hello World Example is

      normally the first one that is used in

      every programming language.  

  *****************************************/

  $helloWorld = "Hello World";

  print "<html>";

  print "<head><title>My First PHP Page</title></head>";

  print "<body>";

  print "<h1>$helloWorld</h1>";

  print "</body>";

  print "<html>";

 

?>

 

§         Invoke IE 5 on your machine, and check your html file with the following address:

http://redhat.ics.metrostate.edu/~f04325??/lab5/hello1.php

 

 

2.         Hello World using the echo statement.

§         Log onto "redhat.ics.metrostate.edu" using your class account, then do the following:

$ cd

$ cd public_html

$ cd lab5

§         Edit an HTML document called "hello2.php" using pico with the following lines:

 

<?php

  /*****************************************

    Author: Your Name

    Date:   Current Date

    Description:

      This PHP script is another way to print

      to the screen.  Using the hello world example

  *****************************************/

  $helloWorld = "Hello World";

  echo "<html>";

  echo "<head><title>My First PHP Page</title></head>";

  echo "<body>";

  echo "<h1>$helloWorld</h1>";

  echo "</body>";

  echo "<html>";

 

?>

 

§         Invoke IE 5 on your machine, and check your html file with the following address:

http://redhat.ics.metrostate.edu/~f04325??/lab5/hello2.php

 

 

3.         Hello World using PHP embedding.

§         Log onto "redhat.ics.metrostate.edu" using your class account, then do the following:

$ cd

$ cd public_html

$ cd lab5

§         Edit an HTML document called "hello3.php" using pico with the following lines:

 

<?php

  /*****************************************

    Author: Your Name

    Date:   Current Date

    Description:

      This PHP script is yet another way to print

      to the screen.  Using the hello world example

  *****************************************/

  $helloWorld = "Hello World";

?>

 

<html>

  <head>

    <title>My First PHP Page</title>

  </head>

  <body>

    <h1><?=$helloWorld?></h1>

  </body>

</html>

 

§         Invoke IE 5 on your machine, and check your html file with the following address:

http://redhat.ics.metrostate.edu/~f04325??/lab5/hello3.php

 

 

4.         Create a Multiplication table using PHP loops

§         Log onto "redhat.ics.metrostate.edu" using your class account, then do the following:

$ cd

$ cd public_html

$ cd lab5

§         Edit an HTML document called "loop.php" using pico with the following lines:

 

<?php

  /*****************************************

    Author: Your Name

    Date:   Current Date

    Description:

   This is a simple mutiplication table.

   If you want to make the table bigger, simply

   change the TBLSIZE value.

    Variables:

   $i index variable

   $j index variable  

   $product product of $i x $j

  *****************************************/

 

  define("TBLSIZE",10); // define a constant for the table size

 

?>

 

<html>

<head>

      <title>A Simple Mutiplication Table</title>

  </head>

  <body>

      <table cols=10 border=1>

         <?php

        for($i=1; $i <= TBLSIZE; $i++){

            echo "<tr>";

            for($j=1; $j <= TBLSIZE; $j++){

               $product = $i * $j;

               echo "<td width=20>$product</td>\n";

            }

            echo "</tr>";

        }

         ?>

      </table>

  </body>

</html>

 

§         Invoke IE 5 on your machine, and check your html file with the following address:

http://redhat.ics.metrostate.edu/~f04325??/lab5/loop.php