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

Slides:



Advertisements
Similar presentations
» PHP arrays are lists of values stored in key-value pairs. » Uses of arrays: Many built-in PHP environment variables. Database functions use arrays.
Advertisements

1 Copyright © 2002 Pearson Education, Inc.. 2 Chapter 5 List Variables and Loops.
Ruby (on Rails) CSE 190M, Spring 2009 Week 2. Arrays Similar to PHP, Ruby arrays… – Are indexed by zero-based integer values – Store an assortment of.
Introduction to Arrays Chapter What is an array? An array is an ordered collection that stores many elements of the same type within one variable.
Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, © Copyright 2010 All Rights Reserved. 1.
Chapter 10 Introduction to Arrays
Creating PHP Pages Chapter 7 PHP Decisions Making.
Objectives Using functions to organize PHP code
PHP Functions and Control Structures. 2 Defining Functions Functions are groups of statements that you can execute as a single unit Function definitions.
Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, © Copyright 2010, All Rights Reserved 1.
Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, © Copyright 2010 All Rights Reserved.
Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, © Copyright 2010 All Rights Reserved. 1.
Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, © Copyright 2014 All Rights Reserved.
Chapter 4 Functions and Control Structures PHP Programming with MySQL.
Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, Copyright © 2014 All Rights Reserved.
Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, © Copyright 2010 All Rights Reserved. 1.
Arrays. A group of data with same type stored under one variable. It is assumed that elements in that group are ordered in series. In C# language arrays.
Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, Copyright © 2010 All Rights Reserved.
Introduction to PHP and MySQL Kirkwood Center for Continuing Education By Fred McClurg, © Copyright 2010, All Rights Reserved.
Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, © Copyright 2010, All Rights Reserved 1.
Saturday May 02 PST 4 PM. Saturday May 02 PST 10:00 PM.
Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, © Copyright 2010, All Rights Reserved 1.
Introduction to PHP and MySQL Kirkwood Center for Continuing Education By Fred McClurg, © Copyright 2014, Fred McClurg, All Rights.
Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, Copyright © 2010 All Rights Reserved. 1.
Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, Copyright © 2010 All Rights Reserved.
Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, © Copyright 2010, All Rights Reserved 1.
Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, © Copyright 2010, All Rights Reserved 1.
Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, © Copyright 2010 All Rights Reserved. 1.
If () else statement, switch statement, while () loop, do…while() loop and for( ; ; ) loop 1.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 23P. 1Winter Quarter Structs and Enumeration.
PHP MOHAMMED SHURRAB TO MISS/ RASHA ATTALLAH. What is PHP? Stands for "PHP Hypertext Preprocessor" Server-side scripting language HTML-embedded Supports.
Using Arrays in PHP (Part I) Lecture # 6. What is an array? It is a collection of multiple values assembled into one variable An array can consist of.
The foreach LooptMyn1 The foreach Loop The foreach loop gives an easy way to iterate over arrays. foreach works only on arrays, and will issue an error.
Copyright © 2003 Pearson Education, Inc. Slide 5-1 The Web Wizard’s Guide to PHP by David Lash.
Looping Constructs “Here we go loop de loop, on a Saturday night” – Johnny Thunder “First I'm up, and then I'm down. Then my heart goes around and around.”
Strings CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Week 7. Lecture 2 Functions, Arrays, PHP&MySQL. Function with More than one argument and a return statement For a function to return a value, the return.
1 DATA STRUCTURES: LISTS. 2 LISTS ARE USED TO WORK WITH A GROUP OF VALUES IN AN ORGANIZED MANNER. A SERIES OF MEMORY LOCATIONS CAN BE DIRECTLY REFERENCED.
Arrays Part 9 dbg. Arrays An array is a fixed number of contiguous memory locations, all containing data of the same type, identified by one variable.
Data collections Android Club Agenda Array ArrayList HashMap.
Pascal language Slides of Omar Al-Nahal. Components of Pascal Language Components of Pascal Language 1. Pascal Character set: - English Letters. - Decimal.
Structures in C++ UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) 1 ADNAN BABAR MT14028 CR.
HTML Table and PHP Array
PHP Programming with MySQL Slide 4-1 CHAPTER 4 Functions and Control Structures.
Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, Copyright © 2015, Fred McClurg, All Rights.
A Practical Guide to Fedora and Red Hat Enterprise Linux Unit 9: Linux Network Services Chapter 16: Administration Tasks By Fred R. McClurg Linux Operating.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 23P. 1Winter Quarter Structs and Enumeration Lecture 23.
Chapter 2 Functions and Control Structures PHP Programming with MySQL 2 nd Edition.
Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, © Copyright 2015, Fred McClurg, All Rights.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 8 Working.
Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, © Copyright 2015, Fred McClurg, All Rights.
 In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.  PHP Loops :  In.
 An array stores multiple values in one single variable.  Example: Output: I like Honda Civic, BMW and Toyota.
Lecture 7 Computer Programming -1-. Conditional Statements 1- if Statement. 2- if ….. else Statement. 3- switch.
CSC 4630 Meeting 17 March 21, Exam/Quiz Schedule Due to ice, travel, research and other commitments that we all have: –Quiz 2, scheduled for Monday.
Introduction to PHP 1.What is PHP? What Is PHP?  php: hypertext preprocessor  Server-side scripting language—like ASP—scripts are executed on server.
Week 6 - Friday.  What did we talk about last time?  Loop examples.
Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, © Copyright 2015, Fred McClurg, All Rights.
The birthday calendar 2B : Unit 6 Days of the week Days of the week.
JavaScript, Sixth Edition
Creating PHP Pages Chapter 10 PHP Arrays. Arrays An array can store one or more values in a single variable name. An element of an associative accessed.
CSC 4630 Perl 3 adapted from R. E. Beck. Problem But we worked on it first: Input: Read from a text file named in a command line argument Output: List.
Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Third Edition by Tony Gaddis.
Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, © Copyright 2016, Fred McClurg, All Rights.
Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, Copyright © 2016, Fred McClurg, All Rights.
Introduction to programming in java Lecture 21 Arrays – Part 1.
Introduction to Programming the WWW I CMSC Winter 2004 Lecture 8.
Kirkwood Center for Continuing Education
Kirkwood Center for Continuing Education
ITM 352 Simple Arrays.
Presentation transcript:

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

Chapter Six Array: A drop of golden sun or “Hi Ho Silver, Array!” or Up, up and Array hp/slides/chapter06a.numeric.ppt 2

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

Chapter Six Numeric Arrays 4

Description: Numeric arrays in PHP are zero based. This means the first array index starts at zero. In other words, the first element resides at 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

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

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

Chapter Six Looping Numeric Indexed Arrays 8

Example:<?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 ", printf( "\$color[%d]: %s ", $i, $color[$i] ); $i, $color[$i] ); }?> “for” with Numeric Index Array 9 What would be printed?

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

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

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

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

<?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; else else $rot13 = ord( $chr ) + 13; $rot13 = ord( $chr ) + 13; printf( "%s = %s ", printf( "%s = %s ", $chr, chr( $rot13 ) ); $chr, chr( $rot13 ) ); }?> Solution 6.1a Numeric Array 14

<?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 ", printf( "%s = %s ", $chr, $rot13 ); $chr, $rot13 ); }?> Solution 6.1b Numeric Array 15

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

to be continued... p/slides/chapter06b.associative.ppt p/slides/chapter06b.associative.ppt 17