kirkwood center for continuing education introduction to php and mysql by fred mcclurg,...

Post on 13-Dec-2015

220 Views

Category:

Documents

4 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Kirkwood Center for Kirkwood Center for Continuing EducationContinuing Education

Introduction toIntroduction toPHP and MySQLPHP and MySQL

By Fred McClurg, frmcclurg@gmail.comBy Fred McClurg, frmcclurg@gmail.com

© Copyright 2015, Fred McClurg, All Rights Reserved.© Copyright 2015, Fred McClurg, All Rights Reserved.

Chapter SixChapter SixArray: A drop of Array: A drop of golden sungolden sun

or “Hi Ho Silver, Array!”or “Hi Ho Silver, Array!”

or Up, up and Arrayor Up, up and Array

http://cecert.kirkwood.edu/http://cecert.kirkwood.edu/~fmcclurg/courses/php/slides/~fmcclurg/courses/php/slides/chapter06a.numeric.pptchapter06a.numeric.ppt

2

Array: Used for storing a collection of Array: Used for storing a collection of values that are referenced by a common values that are referenced by a common variable name and an index value.variable name and an index value.

Analogy: Weekly pill box with Analogy: Weekly pill box with ““SMTWTFSSMTWTFS”” on individual lids.on individual lids.

Element: The individual data values that are Element: The individual data values that are stored in an array.stored in an array.

Index: The reference used to access the Index: The reference used to access the element location.element location.

Array Definition of TermsArray Definition of Terms

3

Chapter SixChapter SixNumeric ArraysNumeric Arrays

4

Description: Numeric arrays in PHP are zero Description: Numeric arrays in PHP are zero based. This means the first array index based. This means the first array index starts at zero. In other words, the first starts at zero. In other words, the first element resides at index zero, the second element resides at index zero, the second element is at index one, etc.element is at index one, etc.

Pitfall: The last index of the array is the Pitfall: The last index of the array is the number of elements minus one. Failure number of elements minus one. Failure to take this into consideration results in to take this into consideration results in the common the common ““off by oneoff by one”” error. error.

Numeric ArraysNumeric Arrays

5

Initialization via multiple statements:Initialization via multiple statements:$weekday[] = "Sunday";$weekday[] = "Sunday";$weekday[] = "Monday";$weekday[] = "Monday";$weekday[] = "Tuesday";$weekday[] = "Tuesday";

Same As:Same As:$weekday[0] = "Sunday";$weekday[0] = "Sunday";$weekday[1] = "Monday";$weekday[1] = "Monday";$weekday[2] = "Tuesday";$weekday[2] = "Tuesday";

Numeric Array Element Numeric Array Element InitializationInitialization

6

Array Initialization via a single statement:Array Initialization via a single statement:$weekday = array($weekday = array( "Sunday", "Monday","Sunday", "Monday", "Tuesday", "Wednesday","Tuesday", "Wednesday", "Thursday", "Friday","Thursday", "Friday", "Saturday" );"Saturday" );

Same As:Same As:$weekday[0] = "Sunday";$weekday[0] = "Sunday";$weekday[1] = "Monday";$weekday[1] = "Monday";$weekday[2] = "Tuesday";$weekday[2] = "Tuesday";......

Numeric Array Initialization Numeric Array Initialization ConstructConstruct

7

Chapter SixChapter SixLooping NumericLooping NumericIndexed ArraysIndexed Arrays

8

Example:

<?php<?php $color = array( "red",$color = array( "red", "green","green", "blue" );"blue" );

// obtain array length// obtain array length $size = count( $color );$size = count( $color );

for ( $i = 0; $i < $size; $i++ )for ( $i = 0; $i < $size; $i++ ) {{ printf( "\$color[%d]: %s<br />",printf( "\$color[%d]: %s<br />", $i, $color[$i] );$i, $color[$i] ); }}?>?>

““forfor”” with Numeric Index with Numeric Index ArrayArray

9

What would be printed?

What would be printed?

Description: The Description: The ““foreachforeach”” construct was construct was specifically designed to work with arrays. specifically designed to work with arrays. It functions as an iterator that traverses It functions as an iterator that traverses each element of the array sequentially.each element of the array sequentially.

Preferred Usage:Preferred Usage:1.1. When the element value is needed.When the element value is needed.

2.2. When the associated element index When the associated element index number is not used.number is not used.

3.3. When every element of the array needs When every element of the array needs to be accessed.to be accessed.

““foreachforeach”” with Numeric Arrays with Numeric Arrays

10

Syntax:

foreach ( $array as $value ){ statement; ...}

““foreachforeach”” Numeric Array Numeric Array SyntaxSyntax

11

ExampleExample::

<?php<?php $colors = array( "red", $colors = array( "red", "green","green", "blue" );"blue" );

foreach ( $colors as $name )foreach ( $colors as $name ) echo "$name<br />";echo "$name<br />";?>?>

““foreachforeach”” Numeric Index Numeric Index ExampleExample

12

What would be printed?

What would be printed?

Assignment: Write a PHP program that prints Assignment: Write a PHP program that prints the ROT13 values for letters of the alphabet.the ROT13 values for letters of the alphabet.

Output: Should look similar to the following:Output: Should look similar to the following:a = na = nb = ob = o......z = mz = m

Hint: Use the Hint: Use the ““range()range()”” function to initialize function to initialize array.array.

Exercise 6.1 Numeric ArrayExercise 6.1 Numeric Array

13

<?php<?php

$letters = range( 'a', 'z' );$letters = range( 'a', 'z' );

foreach ( $letters as $chr )foreach ( $letters as $chr )

{{

if ( $chr > 'm' )if ( $chr > 'm' )

$rot13 = ord( $chr ) - 13;$rot13 = ord( $chr ) - 13;

elseelse

$rot13 = ord( $chr ) + 13;$rot13 = ord( $chr ) + 13;

printf( "%s = %s<br />",printf( "%s = %s<br />",

$chr, chr( $rot13 ) );$chr, chr( $rot13 ) );

}}

?>?>

Solution 6.1a Numeric ArraySolution 6.1a Numeric Array

14

<?php<?php $letters = range( 'a', 'z' );$letters = range( 'a', 'z' );

foreach ( $letters as $chr )foreach ( $letters as $chr ) {{ $rot13 = str_rot13($chr);$rot13 = str_rot13($chr); printf( "%s = %s<br />",printf( "%s = %s<br />", $chr, $rot13 );$chr, $rot13 ); }}?>?>

Solution 6.1b Numeric ArraySolution 6.1b Numeric Array

15

ExampleExample::

<?php<?php function printList( $list )function printList( $list ) {{ foreach( $list as $item )foreach( $list as $item ) {{ printf( "%s<br />", $item );printf( "%s<br />", $item ); }} }} $groceries = array( "blueberries", $groceries = array( "blueberries", "strawberries","strawberries", "bananas" );"bananas" );

printList( $groceries );printList( $groceries );?>?>

Passing an Array to a FunctionPassing an Array to a Function

16

What would be printed?

What would be printed?

to be to be continued ...continued ...

http://cecert.kirkwood.edu/~fmcclurg/courses/php/slides/chapter06b.associative.ppt

17

top related