150 likes | 304 Views
PHP. Functions Composite Types. Functions. Declaration function functionName(paramList) { /* code goes here */ } paramList is comma-separated list of param names with optional default values use return to return a value Calling a function functionName( argList)
E N D
PHP Functions Composite Types
Functions • Declaration • function functionName(paramList) { • /* code goes here */ } • paramList is comma-separated list of param names with optional default values • use return to return a value • Calling a function • functionName( argList) • argList is comma-separated list of expressions - number must match declaration
Variable scope • Variables defined outside of any function are global • Variables defined inside a function are local to the function (as are parameters) • Using global variables in a function • $GLOBALS is array containing all global variables • global keyword
Compound Types in PHP • PHP has two compound types • Arrays -arrays in PHP are really ordered maps • multi-dimensional arrays are arrays whose elements are arrays • Objects - variables whose type is defined by a class
Arrays • An array consists of a collection of elements • each element has a key and a value (like a hash table) • arrays with only numeric keys are a special case • you can use arrays with numeric keys the same way you do an array in C or Java
Creating an array • Create dynamically by assigning values to elements • $veggie['corn'] = 'yellow'; • $dinner[0] = 'Lemon Chicken'; • Use the Array constructor • $veggie = array( 'corn' => 'yellow', 'beet' => 'red', 'carrot' => 'orange');
Creating a numeric array • Use array with just a list of values • keys will automatically be numbers (starting from 0) $dinner = ('Sweet Corn and Asparagus', 'Lemon Chicken', 'Spicy Eggplant'); • Add new elements to end of list by assigning with no index • $dinner[] = 'Braised Bamboo fungus';
Using arrays • count($arrayName) gives the number of elements in the array • implode(delim, array) creates a string from an array • $menu = implode( ', ', $dinner); • explode(delim, string) creates a numeric array from a string • $dinner = explode( ', ', $menu);
Sorting arrays • The array elements will be rearranged by these methods • sort()/rsort() sort by element values (ascending/descending) • asort()/arsort() sort by element value, keeping keys and values together • ksort()/krsort() sort by key, keeping keys and values together
Looping with arrays • Use foreach to loop through elements of array foreach( $meal as $key => $value) print "$key $value\n"; foreach( $dinner as $dish) print "You can eat $dish.\n" • foreach goes through elements in order they were added to array • not necessarily numeric order
Classes and Objects • PHP supports object-oriented programming • Class is a template describing both data and operations • Method is a function defined in a class • Property is a variable defined in a class • Constructor is used to create instances (objects) • Static method doesn't need an object
Sample Class definition class Cart { var $items;// Items in our shopping cart // Add $num articles of $artnr to the cart function add_item($artnr, $num) { $this->items[$artnr] += $num;} // Take $num articles of $artnr out of the cart function remove_item($artnr, $num) { if ($this->items[$artnr] > $num) { $this->items[$artnr] -= $num; return true; } elseif ($this->items[$artnr] == $num) { unset($this->items[$artnr]); return true; } else { return false; } }
Constructors • Constructor is a method with the same name as the class • Use it to initialize properties class Item { var $catalogNum, $price; function Item($id='none', $cost='0.0') { $catalogNum=$id; $price = $cost; } … }
Using Objects • Use new to create objects • $myItem = new Item( 'AB234', 3.95); • Access methods and properties with -> operator • $charge = $myItem->price;
Sources • Learning PHP 5 by David Sklar • Programming PHP by Rasmus Ledorf and Kevin Tatroe • PHP home page • http://www.php.net/