intTypePromotion=1
zunia.vn Tuyển sinh 2024 dành cho Gen-Z zunia.vn zunia.vn
ADSENSE

Web engineering: Lecture 21, 22 - Majid Mumtaz

Chia sẻ: You You | Ngày: | Loại File: PDF | Số trang:24

35
lượt xem
3
download
 
  Download Vui lòng tải xuống để xem tài liệu đầy đủ

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.

Chủ đề:
Lưu

Nội dung Text: Web engineering: Lecture 21, 22 - Majid Mumtaz

  1. Web Engineering Lecture 21-22 MAJID MUMTAZ Department of Computer Science, CIIT Wah 1
  2. 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
  3. 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
  4. PHP Arrays • The foreach loop statement: Is very handy to loop through arrays • Syntax foreach(array as value) { Code to be executed; } Example 4
  5. 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
  6. 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
  7. 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
  8. 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
  9. 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
  10. 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
  11. 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
  12. 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
  13. 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
  14. 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
  15. Examples There is a function available in PHP i.e count() that can be used to give you total number of array elements. 15
  16. PHP Include() and require() functions • Include() function takes all the test in a specified file and copies it into the file that uses the include function. Syntax: include(”header.php”); It is useful for modular programming. Another function that is similar to include is the require() function: syntax is require (”head.php”); 16
  17. Difference between include() and require() function • The two functions are identical in every way, except how they handle errors – The include() function generates a warning ( but the script will continue execution) – Require() function generates a fatal error ( and the script execution will stop after the error) Note: Recommended function is require(). 17
  18. PHP Date() function • Date() function is used to format a time or a date. The PHP date() function formats a timestamp to a more readable date and time. Syntax: date(format, timestamp); The first parameter in the date() function specifies how to format the date/time. It usese letters to represent date and time format. Some of the letter that can be used: – D – the day of the month (0 1– 31) – M – the current month, as a number (01 – 12) – Y – the current year in four digits. – 1 - Represents the day of the week Note: Other characters, like"/", ".", or "-" can also be inserted between the characters to add additional formatting. 18
  19. PHP Date() function • What is a timestamp: Time stamp is the number of second since January 1, 1970 at 00:00:00 GMT. This is also known as the unix timestamp. Or • A timestamp is an integer that holds the number of seconds from january 1, 1970. • The mktime() function returns the unix timestamp for a specified date. Syntax: mktime(hour, minute, second, month, day, year) 19
  20. PHP forms & user input • The php $_GET & $_POST variable are used to retrieve information from forms, like user input. • PHP $_GET: The $_GET variable is used to collect values from a form with method=”GET”. • The $_GET variable is used to collect values from a form with a method=”GET”. Information sent from a form with the GET method is visible to everyone (It will be displayed in the browser’s address bar) and it has limits on the amount of information to send (max. 100 characters) 20
ADSENSE

CÓ THỂ BẠN MUỐN DOWNLOAD

 

Đồng bộ tài khoản
2=>2