Back to DFS's Workshop Page
Back to Agenda Page
If a segment of code is used repeatedly, it is almost always advantageous to replace it with a function. This shortens the overall code length and, if you pick an appropriate name, it makes the code more readable for the programmer.
This page will demonstrate two ways to have a function provide changes to values passed to it.
A function can be passed a value which can be modified locally. This value can then be returned, leaving the original variable unaltered.
The following code is a modification of that given in the second version of the area-of-a-triangle using-files script.
<?php
// trifilefunctret.php
//This script illustrates how to declare and use a function
// which accepts an argument and returns a value
function delete_newline($str) {
// Accepts a string and removes \n from the end if it exists
// Returns the (un)modified string
if( ord( $str[ strlen($str) -1 ] ) == 0x0A ) {
$str = substr( $str, 0, strlen($str) - 1 );
}
return $str;
}
// Open file
$fp = fopen( "trianglenumbers.dat", "r");
// Read, clean & print the base
$b = fgets( $fp, 10 );
$b_clean = delete_newline($b);
echo "b is $b_clean.\n";
// Read, clean & print the height
$h = fgets( $fp, 10 );
$h_clean = delete_newline($h);
echo "h is $h_clean.\n";
// Close file
fclose($fp);
// Calculate and print result
$A = $b_clean * $h_clean / 2;
echo "The area of a triangle with a base of $b_clean and a height of $h_clean is $A.\n";
?>
$b_clean = delete_newline($b);the value of $b will not be changed. If $b is terminated with a newline, $b_clean will be one character shorter.
Frequently, as in this case, it is preferable to actually change the value of the original variable.
This could be done using the code above by assigning the value returned by delete_newline to the original variable.
$b = delete_newline($b);
It can also be accomplished without the assignment operator in the main code. This is done by informing the function that the variable passed to it can be modified. Examine the code for the function below, comparing it to that above.
<?php
//This script illustrates how to declare and use a function
// which accepts an argument and changes its value
function delete_newline(&$str) {
// Accepts a string and removes \n from the end if it exists
// Returns the (un)modified string
if( ord( $str[ strlen($str) -1 ] ) == 0x0A ) {
$str = substr( $str, 0, strlen($str) - 1 );
}
}
// Open file
$fp = fopen( "trianglenumbers.dat", "r");
// Read, clean & print the base
$b = fgets( $fp, 10 );
delete_newline(&$b);
echo "b is $b.\n";
// Read, clean & print the height
$h = fgets( $fp, 10 );
delete_newline(&$h);
echo "h is $h.\n";
// Close file
fclose($fp);
// 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";
?>
delete_newline(&$b);the address of $b is passed to delete_newline. The use of the ampersand (&) is mirrored in the function header.
function delete_newline(&$str)All subsequent references to $str inside the function actually refer to $b.