Presentation is loading. Please wait.

Presentation is loading. Please wait.

Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, © Copyright 2010 All Rights Reserved. 1.

Similar presentations


Presentation on theme: "Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, © Copyright 2010 All Rights Reserved. 1."— Presentation transcript:

1 Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, frmcclurg@gmail.com © Copyright 2010 All Rights Reserved. 1

2 Chapter Six Array: A drop of golden sun or “Hi Ho Silver, Array!” or Up, up and Array http://webcert.kirkwood.edu/~fmcclurg/c ourses/php/slides/chapter06.numeric.ppt 2

3 Array: Used for storing a collection of values that are referenced by a common variable name and an index value. Analogy: Weekly pill box with “SMTWTFS” on individual lids. Element: The individual data values that are stored in an array. Index: The reference used to access the element location. Array Definition of Terms 3

4 Chapter Six Numeric Arrays 4

5 Description: Numeric arrays in PHP are zero based. This means the first array index starts at zero. In other words, the first element uses index zero, the second element is at index one, etc. Pitfall: The last index of the array is the number of elements minus one. Failure to take this into consideration results in the common “off by one” error. Numeric Arrays 5

6 Initialization via multiple statements: $weekday[] = "Sunday"; $weekday[] = "Monday"; $weekday[] = "Tuesday"; Same As: $weekday[0] = "Sunday"; $weekday[1] = "Monday"; $weekday[2] = "Tuesday"; Numeric Array Element Initialization 6

7 Array Initialization via a single statement: $weekday = array( "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" ); Same As: $weekday[0] = "Sunday"; $weekday[1] = "Monday"; $weekday[2] = "Tuesday";... Numeric Array Initialization Construct 7

8 Chapter Six Looping Numeric Indexed Arrays 8

9 Example: <?php $color = array( "red", "green", "blue" ); // obtain array length $size = count( $color ); for ( $i = 0; $i < $size; $i++ ) { printf( "\$color[%d]: %s ", $i, $color[$i] ); } ?> “for” with Numeric Index Array 9

10 Description: The “foreach” construct was specifically designed to work with arrays. It functions as an iterator that traverses each element of the array sequentially. Preferred Usage: 1. When the element value is needed. 2. When the associated element index number is not used. 3. When every element of the array needs to be accessed. “foreach” with Numeric Arrays 10

11 Syntax: foreach ( $array as $value ) { statement;... } “foreach” Numeric Array Syntax 11

12 Example: <?php $colors = array( "red", "green", "blue" ); foreach ( $colors as $name ) echo "$name "; ?> “foreach” Numeric Index Example 12

13 Assignment: Write a PHP program that prints the ROT13 values for letters of the alphabet. Output: Should look similar to the following: a = n b = o... z = m Hint: Use the “range()” function to initialize array. Exercise 6.1 Numeric Array 13

14 <?php $letters = range( 'a', 'z' ); foreach ( $letters as $chr ) { if ( $chr > 'm' ) $rot13 = ord( $chr ) - 13; else $rot13 = ord( $chr ) + 13; printf( "%s = %s ", $chr, chr( $rot13 ) ); } ?> Solution 6.1a Numeric Array 14

15 <?php $letters = range( 'a', 'z' ); foreach ( $letters as $chr ) { $rot13 = str_rot13($chr); printf( "%s = %s ", $chr, $rot13 ); } ?> Solution 6.1b Numeric Array 15

16 Example: <?php function printList( $list ) { foreach( $list as $item ) { printf( "%s ", $item ); } $groceries = array( "blueberries", "strawberries", "bananas" ); printList( $groceries ); ?> Passing an Array to a Function 16

17 to be continued... http://webcert.kirkwood.edu/~fmcclurg/co urses/php/slides/chapter06b.associative.ppt


Download ppt "Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, © Copyright 2010 All Rights Reserved. 1."

Similar presentations


Ads by Google