KEMBAR78
Array andfunction | DOCX
1
Agenda
 Array
 Object
 Null
 Function
An array is a special type of variable that can hold many
values at once, all accessible via a single variable name.
Arrays are very useful whenever you need to work with large
amounts of data — such as records from a database — or
group related data together.
• There are three types of arrays thatyou can create. These are:
• Indexedarray — An array with a numeric key.
• Associative array — An array whereeach key has its own specific value.
• Multidimensional array — An array containing oneor morearrays within
itself.
1. An indexed or numeric array stores each array element with a numeric index. The
following examples shows two ways of creating an indexed array, the easiest way is:
• Note:In an indexedornumericarray,the indexesare automaticallyassignedandstartwith 0,
and the valuescanbe anydata type.
2. In an associative array, the keys assigned to values can be arbitrary
and user defined strings. In the following example the array uses
keys instead of index numbers:
2
3. The multidimensional array is an array in which each element can also be an array and
each element in the sub-array can be an array or further contain array within itself and so
on. An example of a multidimensional array will look something like this:
1. <?php
2. //associative multidimensional array
$mark =array("Abe"=>array( "physics"=>89,"math"=>80,"english"=>90),
"keb"=>array("physics"=>95,"math"=>85,"english"=>95));
3. echo "Abe's physics mark is ". $mark["Abe"]["physics"]."<br>";
4. //indexed multidimensional array
$number = array(array(50,60,70),
array(80,90,40));
5. echo $number[0][0];
6. echo $number[0][1];
7. echo $number[0][2]."<br>";
8. echo $number[1][0];
9. echo $number[1][1];
10. echo $number[1][2]."<br>";
11. echo $number[2][0];
12. echo $number[2][1];
13. echo $number[2][2];
14. ?>
3
4
1. <!DOCTYPE html>
2. <html>
3. <head>
4. <title>Associative
Array</title>
5. </head>
6. <body>
7. <?php
8. $age =
array("Abebe"=>20,
"Kebede"=>14,
"Yohannes"=>45,
"Abay"=>35);
9. asort($age);
10. ?>
11. <table>
12. <tr>
13. <td><?php echo
"Abebe is ".
$age['Abebe']." years
old";?></td>
14. </tr>
15. <tr>
16. <td><?php echo "Sort
by age ascending by
value <br>";?></td>
17. </tr>
18. <tr>
19. <td><?php
print_r($age);?></td>
20. </tr>
21. <tr>
22. <td>
23. Age sort descending
by value
24. </td>
25. </tr>
26. <tr>
27. <td>
28. <?php
29. arsort($age);
30. echo print_r($age);
31. ?>
32. </td>
5
33. </tr>
34. <tr>
35. <td><?php echo "Sort
by age ascending by
key";?></td>
36. </tr>
37. <tr>
38. <td><?php
39. ksort($age);
40. print_r($age);?></td>
41. </tr>
42. <tr>
43. <td>
44. Age sort descending by key
45. </td>
46. </tr>
47. <tr>
48. <td>
49. <?php
50. krsort($age);
51. echo print_r($age);
52. ?>
53. </td>
54. </tr>
55. </table>
56. </body>
57. </html>
6
PHP Function
• A function is a named block of code that is designed to perform a
specific task.
• Once a function is defined, you can reuse it without copying and
pasting a code block again and again.
• A function may accept one or more arguments, which are the
values that you pass to the function.
• A function may return a value so that the calling script can
communicate with it.
Function parameters
• Informationcanbe passedto functionsthrougharguments.Anargumentisjustlike avariable.
• Functionmayaccept one or more parameters.There are twoways to passparameterstoa
function:passingparameterbyvalue andpassingparameterbyreference.
• Parameter iswhat's giveninthe functiondeclaration/definition.
• Argument iswhat'spassedwhencallingthe function.
7
Benefits of using function
• Functions are reusable– Because a function is designed to performa
specific independent task so that it can be reused in other web
applications.
• Functions help avoid duplicate code – A function helps avoid copying
and pasting code all over places by wrapping the logic and assigning it a
name.
• Functions make your scriptmodular – by using functions, a big script is
divided into many functions that are easier to build, test and maintain.
Object
• <?php
• classCar {
• functionCar() {
• $this->model ="VW";
• }
• }
• // create an object
• $herbie = newCar();
• // showobjectproperties
• echo$herbie->model;
• ?>
8
Null
• Null isa special datatype whichcan have onlyone value:NULL.
• A variable of datatype NULL isa variable that has no value assignedtoit.
• If a variable iscreated withoutavalue,itisautomaticallyassignedavalue of NULL.
• Variablescanalsobe emptiedbysettingthe value toNULL:

Array andfunction

  • 1.
    1 Agenda  Array  Object Null  Function An array is a special type of variable that can hold many values at once, all accessible via a single variable name. Arrays are very useful whenever you need to work with large amounts of data — such as records from a database — or group related data together. • There are three types of arrays thatyou can create. These are: • Indexedarray — An array with a numeric key. • Associative array — An array whereeach key has its own specific value. • Multidimensional array — An array containing oneor morearrays within itself. 1. An indexed or numeric array stores each array element with a numeric index. The following examples shows two ways of creating an indexed array, the easiest way is: • Note:In an indexedornumericarray,the indexesare automaticallyassignedandstartwith 0, and the valuescanbe anydata type. 2. In an associative array, the keys assigned to values can be arbitrary and user defined strings. In the following example the array uses keys instead of index numbers:
  • 2.
    2 3. The multidimensionalarray is an array in which each element can also be an array and each element in the sub-array can be an array or further contain array within itself and so on. An example of a multidimensional array will look something like this: 1. <?php 2. //associative multidimensional array $mark =array("Abe"=>array( "physics"=>89,"math"=>80,"english"=>90), "keb"=>array("physics"=>95,"math"=>85,"english"=>95)); 3. echo "Abe's physics mark is ". $mark["Abe"]["physics"]."<br>"; 4. //indexed multidimensional array $number = array(array(50,60,70), array(80,90,40)); 5. echo $number[0][0]; 6. echo $number[0][1]; 7. echo $number[0][2]."<br>"; 8. echo $number[1][0]; 9. echo $number[1][1]; 10. echo $number[1][2]."<br>"; 11. echo $number[2][0]; 12. echo $number[2][1]; 13. echo $number[2][2]; 14. ?>
  • 3.
  • 4.
    4 1. <!DOCTYPE html> 2.<html> 3. <head> 4. <title>Associative Array</title> 5. </head> 6. <body> 7. <?php 8. $age = array("Abebe"=>20, "Kebede"=>14, "Yohannes"=>45, "Abay"=>35); 9. asort($age); 10. ?> 11. <table> 12. <tr> 13. <td><?php echo "Abebe is ". $age['Abebe']." years old";?></td> 14. </tr> 15. <tr> 16. <td><?php echo "Sort by age ascending by value <br>";?></td> 17. </tr> 18. <tr> 19. <td><?php print_r($age);?></td> 20. </tr> 21. <tr> 22. <td> 23. Age sort descending by value 24. </td> 25. </tr> 26. <tr> 27. <td> 28. <?php 29. arsort($age); 30. echo print_r($age); 31. ?> 32. </td>
  • 5.
    5 33. </tr> 34. <tr> 35.<td><?php echo "Sort by age ascending by key";?></td> 36. </tr> 37. <tr> 38. <td><?php 39. ksort($age); 40. print_r($age);?></td> 41. </tr> 42. <tr> 43. <td> 44. Age sort descending by key 45. </td> 46. </tr> 47. <tr> 48. <td> 49. <?php 50. krsort($age); 51. echo print_r($age); 52. ?> 53. </td> 54. </tr> 55. </table> 56. </body> 57. </html>
  • 6.
    6 PHP Function • Afunction is a named block of code that is designed to perform a specific task. • Once a function is defined, you can reuse it without copying and pasting a code block again and again. • A function may accept one or more arguments, which are the values that you pass to the function. • A function may return a value so that the calling script can communicate with it. Function parameters • Informationcanbe passedto functionsthrougharguments.Anargumentisjustlike avariable. • Functionmayaccept one or more parameters.There are twoways to passparameterstoa function:passingparameterbyvalue andpassingparameterbyreference. • Parameter iswhat's giveninthe functiondeclaration/definition. • Argument iswhat'spassedwhencallingthe function.
  • 7.
    7 Benefits of usingfunction • Functions are reusable– Because a function is designed to performa specific independent task so that it can be reused in other web applications. • Functions help avoid duplicate code – A function helps avoid copying and pasting code all over places by wrapping the logic and assigning it a name. • Functions make your scriptmodular – by using functions, a big script is divided into many functions that are easier to build, test and maintain. Object • <?php • classCar { • functionCar() { • $this->model ="VW"; • } • } • // create an object • $herbie = newCar(); • // showobjectproperties • echo$herbie->model; • ?>
  • 8.
    8 Null • Null isaspecial datatype whichcan have onlyone value:NULL. • A variable of datatype NULL isa variable that has no value assignedtoit. • If a variable iscreated withoutavalue,itisautomaticallyassignedavalue of NULL. • Variablescanalsobe emptiedbysettingthe value toNULL: