Presentation is loading. Please wait.

Presentation is loading. Please wait.

LIS651 lecture 3 functions and arrays Thomas Krichel 2010-02-11.

Similar presentations

Presentation on theme: "LIS651 lecture 3 functions and arrays Thomas Krichel 2010-02-11."— Presentation transcript:

1 LIS651 lecture 3 functions and arrays Thomas Krichel

2 today more string functions general function desciption more number functions arrays the foreach() {} statement

3 string functions There are a long list of string functions in the PHP reference manual. When you work with text, you should look at those string functions at Working with text is particularly important when checking the input of users into your form. I am looking at just a few of examples here. You really need to read the reference to see what is available.

4 trim() trim(string) removes the whitespace at the beginning and the end of the string string. It returns the transformed string. $input=" Festbock "; $output=trim($input); print "|$output|";// prints: |Festbock| whitespace is any of the following characters –the blank character –the newline –the carriage return –the tabulation character

5 strlen() strlen(string) returns the length of the string string. $zip=trim($_POST['zipcode']); $zip_length=strlen($zip); print $zip_length; // hopefully, prints 5

6 strip_tags() strip_tags(string) removes HTML tags from the string string $input=" But weiser"; print strip_tags($input); // prints: Butweiser $in=" "; print strip_tags($in); // prints nothing, hurray!

7 htmlspecialchars() htmlspecialchars(string) makes XML entities out of XML special characters in the string string.,&, and " are transformed. It returns the transformed string. $in="What does the element do?"; print htmlspecialchars($in); // prints: What does the <div> element do? Using htmlspecialchars() is considered to be good security because it prevents injection of HTML and especially its element.

8 substr() substr( string, start, offset) returns the substring of a string string starting at position start, with length offset. $string=I like beer.; $sub=substr($string, 2, 4); print $sub; // prints like

9 documentation: description string trim ( string $str [, string $charlist ] ) here "string" at the beginning tells us that the function returns a variable of type string. The name of the function is written in bold. Then comes the parenthesis. It encloses the arguments. Anything that is optional is enclosed in square brackets. Don't put the square bracket in the function call.

10 argument list string $str [, string $charlist ] This argument list suggests –that you have to give a first argument that is a string (or will be converted to it) –that there is an optional argument that also is a string –arguments are separated by comma. The arguments are described in the parameters section.

11 return value There is a section in the description for the returned value. You will be told what type. You will be told what it means.

12 more number functions abs() calculates the absolute value print abs(-3)// prints: 3 print abs(3)// prints: 3 max() and min() return maximum and minimum print min(2,3)// prints: 2 rand( min, max ) returns a random integer between the integers min and max, included. The list of functions that use numbers is

13 variable types Variables in PHP have types. You can check for types is_numeric() is_string() is_int() is_float() They all return a Boolean value. They can be used to check the nature of a variable.

14 arrays The variables we have looked at up until now are scalars. They only contain one piece of data. Arrays are variables that can contain more than one piece of data. –For example, a six pack in conveniently represented as an array of cans of beer. –For another example, a class is a group of people, each having a name, a social security number, etc.

15 numeric arrays An numeric array has key value pairs where the keys are numbers. $good_beers[0]="Baltika 8"; $good_beers[1]="Bruch Festbock"; or as follows $lousy_beers=array("Miller Lite", "Sam Adams", "Budweiser"); print $lousy_beers[0];// prints: Miller Lite print $lousy_beers[2];// prints: Budweiser

16 keeping count in numeric arrays For numeric arrays, you can add members very simple without keeping track of number. $beers=array("Karlsberg", "Bruch") ; $beers[]="Budweiser"; // $beer now has Karlberg, Bruch and Budweiser print count($beers) ; // prints 3

17 string arrays Sometimes you need data structured by a string. For example for a price list. $price['Grosswald Export']=1.45; $price['Bruch Festbock']=1.74; // the array $price has strings as keys An equivalent way to declare this is $price=array('Grosswald Export' => 1.45, 'Bruch Festbock' => 1.74);

18 array functions There is a very large number of array functions. They are described in the array function reference. Now we are just looking at some examples.

19 count() count() returns the size of an array $price['Grosswald Export']=1.45; $price['Bruch Festbock']=1.74; $product_number=count($price); print "We have $product_number products for you today."; // prints: We have 2 products for you today.

20 unset() This can be used to unset an element $beers_drunk('Amstel' => 'good', 'Miller' =>'ok', 'Budweiser'=>'lousy'); unset($beers_drunk('Amstel'); Now the array $beers_drunk only has two elements.

21 foreach() {} loop, numeric array The foreach loop loops over arrays. You use it as foreach($array as $element). The array $array is the array you are looping through. Each time you reach a new element, the current element is placed in $element.

22 a foreach() example $bottles=array('Amstel', 'Karlsberg', 'Sam Adams'); foreach($bottles as $beer) { print "Thomas has a $beer,\n"; } // prints: // "Thomas has a Amstel, // Thomas has a Karlsberg, // Thomas has a Sam Adams,"

23 foreach() loop, string array The foreach loop loops over arrays. You use it as foreach( $array as $key => $value ). The array $array is the array you are looping through. Each time you reach a new element, the current key is placed in $key and the value in $value.

24 another foreach() example Recall the $price string array. Another example illustrates print " \n"; foreach ($price as $item => $euro_amount) { print " $item \n"; print " €$euro_amount \n"; } print " "; This prints the full price list. But it could also do the whole form. This is fabulous!

25 foreach() example from the form $_GET is an array. You can loop through it. foreach($_GET as $control => $value) { print you set $control to $value \n; }

26 the well-aligned price table $l_r=array('left','right'); $count=0; // counter of elements printed print " \n"; foreach ($price as $item => $euro_amount) { print "

27 print " \n"; // This produces something like // // Grosswald Export // €1.45 // Bruch Festbock // €1.74 //

28 Thank you for your attention! Please switch off machines b4 leaving!

Download ppt "LIS651 lecture 3 functions and arrays Thomas Krichel 2010-02-11."

Similar presentations

Ads by Google