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 script below utilizes arguments on the Command Line.
We will use the area-of-a-triangle problem for this demonstration.
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"; ?>
php -f triargsCL.php 5 7
if( $argc < 3 ) die("Too few arguments provided.\n");