Using string functions and defining your own functions

Using string functions



<!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/13/08
-->
<html  xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<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 "<p>\n";
print "String \"$str1\" has ".strlen($str1)." characters<br/>\n";
print "String \"$str2\" has ".strlen($str2)." characters<br/>\n";
print "</p>\n";

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

print "<p>\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 "</p>\n";

print "<h3>Finding if strings are equal</h3>\n";
print "<p>\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 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 different<br/>\n";
 }
print "</p>\n";

print "<h3>More stuff you can do with strings</h3>\n";

print "<p>\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";
print "</p>\n";
?>

</body>
</html>
http://csci1101sp10.morris.umn.edu/~elenam/1101_spring10/functions/using_string_functions.php

Changing 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/3/09
-->

<?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  xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<title>
PHP functions

</title>
<?php 
// defining php functions

// the function prints a string as an h3 header
function print_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
print_header("data"); 
print "<p>";
print_r($data);
print "</p>";
print_header("temperature:");
print "<p>";
print_r($temperature);
print "</p>";

// Calling a function in a loop
print_header("Finding the average of each row of data");
print "<p>";
foreach ($data as $day_data) {
  $average = average($day_data);
  print "The average is $average<br/>\n";
 }
print "</p>";

print_header("Finding the average of each row of temperature");
print "<p>";
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";
}
print "</p>";
?>

</body>
</html>
http://csci1101sp10.morris.umn.edu/~elenam/1101_spring10/functions/functions.php

Exercise on writing 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  xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<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 


?>
</head>
<body>

<h2>Printing out two lists</h2>
<?php
  // print an unordered list of fruit
print_list($fruit);
// print an ordered list of seasons
print_list($seasons);
?>
</body>
</html>
http://csci1101sp10.morris.umn.edu/~elenam/1101_spring10/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  xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<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 "<p>Beginning of the function: $str1, $str2 </p>\n";
  $temp = $str1;
  $str1 = $str2;
  $str2 = $temp;
  print "<p>End of the function: $str1, $str2 </p>\n";
}

?>
</head>
<body>
<?php
sort_strings($string1, $string2);
?>
<table border="1">

<tr>
<?php
print "<td style=\"width: 70%\">$string1 </td><td> $string2 </td>\n";
?>
</tr>
</table>
</body>

</html>
http://csci1101sp10.morris.umn.edu/~elenam/1101_spring10/functions/switch_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  xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<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://csci1101sp10.morris.umn.edu/~elenam/1101_spring10/functions/switch_reference.php

Passing array by reference


<!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/3/09
-->

<?php
function switch_pairs(&$arr) {
  for ($i = 0; ($i + 1) < count($arr); $i = $i + 2) {
    // switching elements at $i and $i+1
    $temp = $arr[$i];
    $arr[$i] = $arr[$i + 1];
    $arr[$i + 1] = $temp;
  }
}
?>
<html  xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<title>
Passing an array by reference
</title>
<p>

<?php 
$data = array(1, 2, 3, 4, 5, 6, 7, 8);

foreach($data as $item) {
  print "$item ";
}
print "<br />";

switch_pairs($data);

foreach($data as $item) {
  print "$item ";
}
print "<br />";
?>
</p>
</body>
</html>
http://csci1101sp10.morris.umn.edu/~elenam/1101_spring10/functions/array_reference.php

UMM CSci 1101