Back to DFS's Workshop Page
Back to Agenda Page
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 scripts below utilize the interactive technique.
We will use the area-of-a-triangle problem for this demonstration.
The following code should be saved in a file called triInteractiveCL1.php.
<?php
// triInteractiveCL1.php
// This script illustrates how to process information
// interactively provided by the user to a script for testing purposes
// using the fgets() function
// Interactively obtain the dimensions $b (base) and $h (height)
// of the triangle
// Get b
echo ("Enter the value for the base: ");
$b = trim(fgets(STDIN));
// Get h
echo ("Enter the value for the height: ");
$h = trim(fgets(STDIN));
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";
?>
php -f triIntreractiveCL1.php
The following code should be saved in a file called triInteractiveCL2.php.
<?php
// triInteractiveCL2.php
// This script illustrates how to process information
// interactively provided by the user to a script for testing purposes
// using the fscanf() function
// Interactively obtain the dimensions $b (base) and $h (height)
// of the triangle
// Get b
echo ("Enter the value for the base: ");
fscanf (STDIN, "%d\n", $b);
// Get h
echo ("Enter the value for the height: ");
fscanf (STDIN, "%d\n", $h);
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";
?>
php -f triIntreractiveCL2.php