1 / 15

PHP

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)

dea
Télécharger la présentation

PHP

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. PHP Functions Composite Types

  2. 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

  3. 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

  4. 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

  5. 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

  6. 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');

  7. 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';

  8. 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);

  9. 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

  10. 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

  11. 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

  12. 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; } }

  13. 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; } … }

  14. Using Objects • Use new to create objects • $myItem = new Item( 'AB234', 3.95); • Access methods and properties with -> operator • $charge = $myItem->price;

  15. Sources • Learning PHP 5 by David Sklar • Programming PHP by Rasmus Ledorf and Kevin Tatroe • PHP home page • http://www.php.net/

More Related