Danh mục

Web engineering: Lecture 21, 22 - Majid Mumtaz

Số trang: 24      Loại file: pdf      Dung lượng: 0.00 B      Lượt xem: 24      Lượt tải: 0    
tailieu_vip

Xem trước 3 trang đầu tiên của tài liệu này:

Thông tin tài liệu:

In this lecture, we will learn PHP Arrays. In PHP there are three type of arrays: Index arrays/Numeric, associative arrays, multidimensional arrays. In lecture 21 and 22, we will learn three type of PHP Arrays. Inviting you to refer.
Nội dung trích xuất từ tài liệu:
Web engineering: Lecture 21, 22 - Majid Mumtaz Web Engineering Lecture 21-22 MAJID MUMTAZ Department of Computer Science, CIIT Wah 1 PHP Arrays • What is an Array? – An array stores multiple value in one signle variable. Or we can say that – ”An array is a special variable, which can hold more than one same types of value”. • In PHP there are three type of arrays 1. Index arrays/Numeric: Array with numeric index. 2. Associative arrays: Arrays with named keys 3. Multidimensional arrays: Arrays containing one or more arrays 2 PHP Arrays • Index arrays/Numeric: A numeric array stores elements with a numeric ID key. The numeric index can be assigned automaticaly, and index will start at 0. • Declaration: $books = array(”php”, ”java”, ”c++”); OR $books[0] = ”php”; $books[1] = ”java”; $books[2] = ”c++”; Example: 3 PHP Arrays • The foreach loop statement: Is very handy to loop through arrays • Syntax foreach(array as value) { Code to be executed; } Example 4 PHP Arrays • Associative arrays: When storing data about specific named values, a numeric array is not always the best way to do it. The alternate solution is associative array. With such arrays, each element is referenced by a string index. Typically a numeric index is called an index and a string index is called a key • Associative arrays are arrays that use named keys that you assign to them when storing data about specific named values e.g. $price = array(”php”=> ”532”, ”java”=> ”340”); OR $price[’php’] = ”532”; $price[’java’] = ”340”; 5 PHP Arrays • Using foreach statement in associative array Example: $price = array(”php”=> ”532”, ”java”=> ”340”); Foreach ($price as $key=>$value) { echo $key. ”price is” . $value; echo ””; } 6 PHP Arrays • Multidimensional Arrays: In MD array, each element in the main array can also be an array, and each element in the sub-array can be an array, and so on • Example: $shop = array(”laptop” => array(”Del”, ”Hp”, ”IBM”), ”printer” => array(”Hp”, ”Canon”)); echo $shop[’laptop’][2]; //IBM 7 PHP Functions • PHP Functions: Group of statement under single name is called functions. • A function is a block of code that can be executed whenever we need it – Why functions are useful • To avoid duplicate code • To make code easier to eliminate errors • Type of functions – User-defined functions: Functions which are created by user or programmer is called user defined functions. – Built-in functions: Built-in functions are also called ready- made functions • The real power of PHP comes from its functions • In PHP there are more than 700 built-in functions available 8 PHP Functions • All function start with the word “function()” • Name the function • Add a ”{”, the function code starts after the opening curly brace • Insert the function code • Add a ”}”, the function is finished by a closing curly brace. At the end you must call a function by using their name. E.g. 9 PHP Functions • Parameters Functions: A parameter is a variable that holds the value passed to it when the function is called • If we want to pass argument/parameter to the function, we place them b/w the parentheses, separating each argument by commas, i.e. – functionName (argument) – functionName(argument1, argument2, ...) Example: Function test($a){ echo ”The value is” . $a; } Test (12); 10 PHP Functions • PHP built-in functions: In PHP there are more than 700 built-in functions available – Some of the built-in functions are • Sqrt() • Count() • Sort() and so on • Example: 11 PHP Functions • Sorting of Arrays: The element in an array can be sorted in alphabetical or numeric order, i.e. Ascending or Descending A-Z Z-A Small to large No. Large to Small No. (1 to 10) (10 to 1) 12 PHP Functions • Sort Function for Arrays Sort () Sorts an indexed array in ascending order Rsort() Sorts an indexed array in descending order Asort() Sorts an associative array in ascending order, according to the value Ksort() Sorts an associative array in ascending order, according to the key Arsort() Sorts an associative array in descending order, according to the value Krsort() Sorts an associative array in descending order, according to the key 13 Examples • It will sort the element of the $books array in ascending alphabetical order Sort the elements of the $numbers array in ascending numerical order 14 Examples There is a function available in PHP i.e count() that can be used to give y ...

Tài liệu được xem nhiều:

Tài liệu cùng danh mục:

Tài liệu mới: