Back to DFS's PHP Page


Files I

The keyboard is not used as a direct means of inputting data for use by a PHP script. There are two frequently used sources for the data needed by a script, a file and a web page. Here, we will look at a simple use of files.

We will continue with the area-of-a-triangle problem. Two program scripts will be discussed -- it should be clear which is the preferred one.

To read data from a file, we need a file. We will NOT create it by writing the data from a script which we write. Instead, create a file in ~YOURID/PHP called trianglenumbers.dat using vi. The file should look like the following.

5
7

Version I

The following script should also be placed in ~YOURID/PHP/trifilebad.php.

<?php
// trifilebad.php
// Purpose: read two values from a file,
//     use them as the base and height of a triangle
//     to calculate and print its area
// N.B. It is necessary to remove the NL from each string

// For reading, open the file which contains the base & height data
$fp = fopen( "trianglenumbers.dat", "r") or die("No such file!\n");

// Read in the first line, assigning the string to $b
$b = fgets( $fp, 10 );
// Print out the value of the base
echo "b is $b.\n";
// Read in the first line, assigning the string to $h
$h = fgets( $fp, 10 );
// Print out the value of the base
echo "h is $h.\n";

// Close the file
fclose($fp);

// Calculate and print the result
$A = $b * $h / 2;
echo "The area of a triangle with a base of $b and a height of $h is $A.\n";
?>

Notes

This program, although not perfect, introduces several new concepts:

Running the Code

After saving both the data file and the PHP file (in the same directory, execute the script by typing:

php -f trifilebad.php

Bad Output

The resulting output should look like:

b is 5
.
h is 7
.
The area of a triangle with a base of 5
 and a height of 7
 is 17.5.

Obviously, this is NOT what was intended, given the stated purpose at the start of the file: all three sentences contain line breaks after the values of the variables read in from disk are printed.

This problem results from two circumstances.

First, the file looks like this, each line being terminated by a newline character:

Hex 35 0A 37 0A
ASCII 5 NL 4 NL

Second, fgets returns both the string and the terminating newline character. Check out the documentation on the fgets page at the official PHP Web Site.

Consequently, when the string is printed, both the number and the newline become output. This causes the line breaks seen above.

Version II

This version removes the newline character after the string is read in.

Note that there are three built-in functions which are called in the added code: ord, strlen and substr. Read the documentation to determine how this code works. Do you see another way of eliminating the newline character?

<?php
// trifilegood.php
// Purpose: read two values from a file,
//     use them as the base and height of a triangle
//     to calculate and print its area
// N.B. It is necessary to remove the NL from each string
// For reading, open the file which contains the base & height data
$fp = fopen( "trianglenumbers.dat", "r") or die("No such file!\n");

// Read in the first line, assigning the string to $b
$b = fgets( $fp, 10 );
// Remove the trailing newline char
if( ord( $b[ strlen($b) -1 ] ) == 0x0A ) {
   $b = substr( $b, 0, strlen($b) - 1 );
}
// Print out the value of the base
echo "b is $b.\n";
// Read in the first line, assigning the string to $h
$h = fgets( $fp, 10 );
// Remove the trailing newline char
if( ord( $h[ strlen($h) -1 ] ) == 0x0A ) {
   $h = substr( $h, 0, strlen($h) - 1 );
}
// Print out the value of the base
echo "h is $h.\n";

// Close the file
fclose($fp);

// Calculate and print the result
$A = $b * $h / 2;
echo "The area of a triangle with a base of $b and a height of $h is $A.\n";
?>

© DFStermole: 2002
Created: 22 Sept 02
Last Modified: 26 Sept 02