Basic I/O in PHP

Recall the main purpose of PHP is as a scripting language to serve dynamic webpages. However, it does support input and output from the standard input/output. There are several ways to print output to the standard output. The keywords print and echo (which are aliases of each other) allow you to print any variable or string. PHP also has a printf() function to allow formatted output. Some examples:

$a = 10;
$b = 3.14;

print $a;
print "The value of a is $a\n";

echo $a;
echo "The value of a is $a\n";

printf("The value of a is %d, and b is %f\n", $a, $b);

There are also several ways to perform standard input, but the easiest is to use fgets (short for file get string) using the keyword STDIN (Standard Input). This function will return, as a string, everything the user enters up to and including the enter key (interpreted as the endline character, \n. To remove the endline character, you can use another function, trim which removes leading and trailing whitespace from a string. A full example:

//prompt the user to enter input
printf("Please enter a number: ");
$a = fgets(STDIN);
$a = trim($a);

Alternatively, lines 3-4 could be combined into one: $a = trim(fgets(STDIN)); The call to fgets waits (referred to as “blocking”) for the user to enter input. The user is free to start typing. When the user is done, they hit the enter key at which point the program resumes and reads the input from the standard input buffer, and returns it as a string value which we assign to the variable $a.

The standard input is unstructured. The user is free to type whatever they want. If we prompt the user for a number but they just start mashing the keyboard giving non-numerical input, we may get incorrect results. We can use the conversion functions mentioned above to attempt to properly convert the values. However, this only guarantees that the resulting variable is of the type we want (integer or floating point value for example). The standard input is not a good mechanism for reading input, but it provides a good starting point to develop a few simple programs.


Converting Units

Let’s write a program that prompts the user to enter a temperature in degrees Fahrenheit and convert it to degrees Celsius using the formula:

C = (F-32) * (5/8)

We begin with the basic script shell with the opening and closing PHP tags and some comments documenting the purpose of our script. It becomes apparent that we’ll need a couple of variables: one to hold the Fahrenheit (input) value and one for the Celsius (output) value. We’ll want to ensure that these are floating point numbers which we can do by making some explicit conversion. We use a printf() statement in the first step to prompt the user for input. In the second step, we’ll use the standard input to read the $fahrenheit variable value from the user. Recall that we can use fgets to read from the standard input, but may have to trim the trailing whitespace. If we want to ensure that the variable $fahrenheit is a floating point value, we can use floatval(). The full source code is below for the Fahrenheit-to-Celsius conversion program in PHP:


 * This program converts Fahrenheit temperatures to
 * Celsius

 //1. Prompt the user for input in Fahrenheit
 printf("Please enter degrees in Fahrenheit: ");

 //2. Read the Fahrenheit value from the standard input
 $fahrenheit = trim(fgets(STDIN));
 $fahrenheit = floatval($fahrenheit);

 //3. Compute the degrees Celsius
 $celsius = ($fahrenheit - 32) * (5/9);

 //4. Print the result to the user
 printf("%f Fahrenheit is %f Celsius\n", $fahrenheit, $celsius);


Licenses and Attributions

Speak Your Mind