Back to DFS's Workshop Page
Back to Agenda Page


Passing Command Line Data to Check a Script

In order to test a script with different values, we need a way to make them accessible to the script. This can be done in two basic ways: from the Command Line or interactively. The script below utilizes arguments on the Command Line.

We will use the area-of-a-triangle problem for this demonstration.

A PHP Script to Handle Information Set on the Command Line

The following code should be saved in a file called triargsCL.php.

<?php
// triargsCL.php
// This script illustrates how to process information
// provided on the Command Line to a script for testing purposes

// Obtain the dimensions $b (base) and $h (height)
// of the triangle from the Command Line
$b = $argv[1];
$h = $argv[2];

echo "Calculating the Area of a Triangle\n";
// Print the base
echo "b is $b.\n";

// Print the height
echo "h is $h.\n";

// Calculate and print 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


© 2005 DFStermole
Created: 30 Oct 05
Last Modified: 30 Oct 05