PHP strings and user-defined functions

Operations with strings


<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<!--
Using PHP string functions
Author: Elena Machkasova elenam@morris.umn.edu
Last modified: 3/2/06
-->
<html>

<head>
<title>
Using PHP string functions
</title>
</head>
<body>
<h2>Some things you can do with strings</h2>
<?php
$str1 = "apples";
$str2 = "bananas and oranges";
print "<h3>Finding the length of a string</h3>\n";
print "String \"$str1\" has ".strlen($str1)." characters<br/>\n";
print "String \"$str2\" has ".strlen($str2)." characters<br/>\n";

print "<h3>Finding which sring is first in \"alphabetical\" order</h3>\n";

if (strcmp($str1, $str2) < 0) {
	print "\"$str1\" is before \"$str2\" in alphabetical order<br/>\n";	
} else {
	print "\"$str2\" is before \"$str1\" in alphabetical order<br/>\n";		
}

$str3 = "Kiwi";
if (strcmp($str1, $str3) < 0) {
	print "\"$str1\" is before \"$str3\" in alphabetical order<br/>\n";	
} else {
	print "\"$str3\" is before \"$str1\" in alphabetical order<br/>\n";		
}

print "<h3>Finding if strings are equal</h3>\n";
$str3 = "apples";
if (strcmp($str1,$str3) == 0) {
	print "\"$str1\" and \"$str3\" are the same - Duh!<br/>\n";	
}
$str3 = "Apples";
if (strcmp($str1,$str3) == 0) {
	print "\"$str1\" and \"$str3\" are the same<br/>\n";	
} else {
	print "But \"$str1\" and \"$str3\" are the different<br/>\n";
}
print "Comparing with strcasecmp:<br/>\n";
if (strcasecmp($str1,$str3) == 0) {
	print "\"$str1\" and \"$str3\" are the same<br/>\n";	
} else {
	print "But \"$str1\" and \"$str3\" are the different<br/>\n";
}

print "<h3>More stuff you can do with strings</h3>\n";
$str4 = strtoupper($str1);
print "$str4 is the uppercase version of $str1<br/>\n";
$str5 = ucfirst($str2);
$str6 = ucwords($str2);
print "Capitalized the first letter in string: $str5<br/>\n";
print "Capitalized the first letter in all words: $str6<br/>\n";
print "The string itself didn't change: $str2<br/>\n";
?>

</body>
</html>
http://rynite.morris.umn.edu/~elenam/1101_fall06/php_examples/strings/strings.php

Defining your own functions

Change the temperature example to use functions


<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<!--
PHP two-dimensional arrays
Author: Elena Machkasova elenam@morris.umn.edu
Last modified: 3/2/06
-->

<?php
$data = array(array(2.8, 6.7, 4.5, 3.7, 5.0),
	      array(6.3, 7.8, 5.6, 8.0, 6.7, 7.9),
	      array(3.4, 5.2, 3.9, 4.6, 3.5));
$temperature = array("2/28/06" => array(24, 32, 41, 27),
		"3/1/06" => array(25, 30, 37, 19),
		"3/2/06" => array(15, 22, 26, 18));
?>
<html>
<head>
<title>
Two-dimensional arrays
</title>

<?php 
// defining php functions

// the function prints a string as an h3 header
function pr_header($string) {
	print "<h3>$string</h3>\n";	
}

// the function computes and returns the average of 
// elements of a one-dimensional array
function average($array) {
	$sum = 0;
	foreach ($array as $element) {
		$sum = $sum + $element;	
	}
	$average = $sum/count($array);
	return $average;
}

// write a function that converts temperature from Fahrenheit to Celsius
// here is the formula: Tc = (Tf-32)*(5/9)
?>
</head>
<body>
<?php
pr_header("data"); 
print_r($data);
pr_header("temperature:");
print_r($temperature);

pr_header("Accessing an element in a 2D array:"); 
print $data[2][3]."<br/>\n";
print $temperature["3/1/06"][1]."<br/>\n";

// we use nested for loops here, we could use foreach instead (see below)
pr_header("Finding the average of each row of data");
for ($i = 0; $i < count($data); $i++) {
	$average = average($data[$i]);
	print "The average for row $i is $average<br/>\n";
}

pr_header("Finding the average of each row of temperature");
foreach ($temperature as $day => $temps) {
	$average = average($temps);
	// need brackets around $average, otherwise it's read as $averageF
	print "The average temperature for $day is {$average}F<br/>\n";
}
?>

</body>
</html>
http://rynite.morris.umn.edu/~elenam/1101_fall06/php_examples/functions/twod_array_functions.php

Using a function to determine which string is longer


<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<!--
PHP functions
Author: Elena Machkasova elenam@morris.umn.edu
Last modified: 3/14/06
-->
<html>

<head>
<title>
Table formatting
</title>
<?php 
$string1 = "A very long string that goes on for a while and then some 
until it finally ends at some point long in the future";
$string2 = "A shorter string";

// a php function 

// the function takes two strings and returns:
//  0 if the lengths of the two strings are within 5 characters of each other,
//  1 if the first string is longer than the second,
//  2 if the second one is longer than the first

function longer($str1, $str2) {
	$diff = strlen($str1) - strlen($str2);
	if ($diff > 5) return 1;
	// return stops the function execution, so there is no 
	// need for an else on the next line
	if ($diff < -5) return 2;
	return 0;
}
?>
</head>
<body>

<table border="1">
<tr>
<?php
$long = longer($string1, $string2);
switch ($long) {
	case 0: 
		print "<td style=\"width: 50%\">\n";
		break;
	case 1: 
		print "<td style=\"width: 70%\">\n";
		break;
	case 2: print "<td style=\"width: 30%\">\n";
		break;
	default: 
		print "<td>\n";
}
print "$string1 </td><td> $string2 </td>\n";
?>

</tr>
</table>
</body>
</html>
http://rynite.morris.umn.edu/~elenam/1101_fall06/php_examples/functions/string_table.php

An exercise on functions


<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<!--
PHP functions
Author: Elena Machkasova elenam@morris.umn.edu
Last modified: 3/14/06
-->
<html>
<head>
<title>
List formatting
</title>
<?php 
$fruit = array("apple", "banana", "orange", "grape", "kiwi"); 
$seasons = array("winter", "spring", "summer", "fall");

// write a function print_list to print
// the array as an ordered list if the boolean is 'true',
// as an unordered list otherwise
?>
</head>
<body>

<h2>Printing out two lists</h2>
<?php
// print an unordered list of fruit
print_list($fruit, false);
// print an ordered list of seasons
print_list($seasons, true);
?>
</body>
</html>
http://rynite.morris.umn.edu/~elenam/1101_fall06/php_examples/functions/list.php

Using a function to switch two strings - first attempt


<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<!--
PHP functions
Author: Elena Machkasova elenam@morris.umn.edu
Last modified: 3/14/06
-->
<html>
<head>
<title>
Table formatting
</title>
<?php 
$string1 = "A shorter string";
$string2 = "A very long string that goes on for a while and then some 
until it finally ends at some point long in the future";

// A function that switches the two strings directly in the 
// calling program so that the longer one is first. 
// the strings are passed by reference
function sort_strings($str1, $str2) {
	print "Beginning of the function: $str1, $str2 <br/>\n";
	$temp = $str1;
	$str1 = $str2;
	$str2 = $temp;
	print "End of the function: $str1, $str2 <br/>\n";
}

?>

</head>
<body>
<table border="1">
<tr>
<?php
sort_strings($string1, $string2);
print "<td style=\"width: 70%\">$string1 </td><td> $string2 </td>\n";
?>

</tr>
</table>
</body>
</html>
http://rynite.morris.umn.edu/~elenam/1101_fall06/php_examples/functions/string_table_not_working.php

Using a function to switch two strings - pass by reference


<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<!--
PHP functions
Author: Elena Machkasova elenam@morris.umn.edu
Last modified: 3/14/06
-->
<html>
<head>
<title>
Table formatting
</title>
<?php 
$string1 = "A shorter string";
$string2 = "A very long string that goes on for a while and then some 
until it finally ends at some point long in the future";

// A function that switches the two strings directly in the 
// calling program so that the longer one is first. 
// the strings are passed by reference
function sort_strings(&$str1, &$str2) {
	$temp = $str1;
	$str1 = $str2;
	$str2 = $temp;
}

?>
</head>

<body>
<table border="1">
<tr>
<?php
sort_strings($string1, $string2);
print "<td style=\"width: 70%\">$string1 </td><td> $string2 </td>\n";
?>
</tr>

</table>
</body>
</html>
http://rynite.morris.umn.edu/~elenam/1101_fall06/php_examples/functions/string_table_reference.php

Switching elements of an array - pass by reference


<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<!--
PHP functions
Author: Elena Machkasova elenam@morris.umn.edu
Last modified: 3/14/06
-->

<html>
<head>
<title>
Table formatting
</title>
<?php 
$strings = array("A shorter string", 
"A very long string that goes on for a while and then some until it 
finally ends at some point long in the future"); 

// a php function 

// the function takes an array of two strings switches 
// them if needed so that the longer one is first. 

// the array is passed by reference (the & in the function header)

function sort_strings(&$str_array) {
	if (strlen($str_array[0]) < strlen($str_array[1])) {
		$temp = $str_array[0];
		$str_array[0] = $str_array[1];
		$str_array[1] = $temp;
	}
}
?>
</head>

<body>
<table border="1">
<tr>
<?php
sort_strings($strings);
print "<td style=\"width: 70%\"> $strings[0] </td><td> $strings[1] </td>\n";
?>

</tr>
</table>
</body>
</html>
http://rynite.morris.umn.edu/~elenam/1101_fall06/php_examples/functions/string_table_array.php
UMM CSci 1101