SlideShare a Scribd company logo
1 of 16
Download to read offline
Intro to Server Side Programming 
Lesson Five
Lists and Dictionaries 
• In computer science, a list (or sequence) is an abstract 
data type that implements an ordered collection of values. 
Each instance of a value on a list is usually called an item, 
entry or element of the list, and if the same value appears 
multiple times in the list, each is considered a distinct item 
of the list. 
• An associative array (or dictionary) is an abstract data 
type composed of a collection of key/value pairs, such 
that each key appears at most once in the collection. 
Pairs can be added or removed, and values can be 
modified or retrieved based on the associated key.
What's The Difference? 
• A list is always ordered, can usually return elements or items by numeric 
index(starting at zero), and might not allow modification of values. A 
decent implementation of a list should provide specific operations for 
adding and removing elements to enable them to be used: 
• push - append to the end(or tail) of the list 
• pop - remove from the tail of the list 
• queue(unshift) - append to the beginning(or head) of the list 
• dequeue(shift) - remove from the head of the list 
• A hash is usually not immutable and may be indeterminately ordered. 
Values can usually be altered.
Arrays 
• An array type is a data type that is meant to describe a collection of values 
or variables, each selected by one or more indices(keys) that can be 
computed at run-time of the program. 
• By default, the array type in PHP generates computed, numerical indices, 
starting with zero, to make a list: 
var_dump(array( 'one', 'two', 3, 4, 4.1, 4.2 )); 
$list = array(); $list[] = 'one'; $list[] = 'two'; 
• The value for the key can also be specified as any scalar literal (neither 
array, object nor resource), often a string 
var_dump(array( 'one' => 1, 2 => 'two' )): 
$list[4] = 'four'; $list['five dot one'] = 5.1;
Array Tricks 
• PHP relies heavily on arrays, so there are a lot of array-specific 
functions, like to use an array as a queue or stack 
• array_unshift(), array_shift() - append to and remove from head 
• array_push(), array_pop() - append to and remove from tail 
• PHP arrays have an "internal position" pointer that can be used as an 
iterator: 
• current(), key() - return the current element or key 
• next(), prev(), end(), reset() - advance forward or back 
• each() - return the current key and value and advance the pointer
Even More Array Tricks 
• array_merge() - merge two or more arrays 
• array_slice() - remove an array section 
• array_splice() - insert or replace a section 
• count(), sizeof() - calculate length 
• array_fill(), array_pad(), range() - generate values 
• str_split(), explode(), implode(), join() - array to string
Assignment 5.1 
Find Some Lists and Arrays
Lists of Lists 
• Open Github 
• Look through your forked projects for examples of lists 
and dictionary definitions and methods 
• Copy and paste your examples into a file named 
"homework-5.1.md" with code delimiters as needed 
• Write comments identifying the lists, keys, indexes and 
other list and dictionary pieces discussed 
• Save, add and commit, then push to Github when done
Recursion
Control Flow Statements 
• Control Flow - refers to the order in which the individual statements, 
instructions or function calls of an imperative or declarative program 
are executed or evaluated. Execution results in a choice being made 
as to which of two or more paths should be followed. 
• Types of Control Flow statements: 
• continuation at a different statement (unconditional branch or jump) 
• execute statements only if some condition is met (conditional branch) 
• execute statements until some conditional is met(loop, conditional branch) 
• execute defined statements and return (sub/co-routines, continuations)s 
• stop executing statements (unconditional halt)
Loops 
• A loop is a sequence of statements which is specified once but which may be carried out several times in 
succession, a specified number of times or indefinitely 
• Specific number of times: 
for ( $count = 0; $count < $max; $count++ ) do_something(); 
• Once per each item in a collection(array): 
foreach ( $collection as $item ) do_something(); 
foreach ($collection as $key => $value ) do_something(); 
• Until some condition is met: 
while ( $condition == true ) do_something(); 
do something(); while ( $condition ); 
• Indefinitely(infinitely): 
while ( true ) do_something(); 
do something(); while ( true);
Infinite Loops 
• An unconditional(or infinite) 
loop returns to a fixed point in 
the diagram, usually the top of 
the workflow. Without a 
breaking statement or escape 
clause, it executes forever.
Conditional Loops 
• A conditional loop (while, do-while, 
for, foreach) returns to a 
condition check 
($count < $max) 
Until the condition evaluates 
FALSE, the loop will continue 
to execute
Diagramming Part Two 
• In your pair, find a section of a project that has some significant looping 
and conditional logic (at least three branches) 
• Individually, sketch a simple workflow diagram of the logic; assemble a 
truth table if needed 
• Discuss differences in your diagrams and make a new diagram and truth 
table for the logic to demonstrate
Assignment 5.2 
Loops and Conditionals
• Create a file called "homework-5.2.md" in your “assignments" folder 
• Find a section in your projects that has some decent looping and branching code: at 
least five branches that you can diagram. 
• Copy and paste your example into your file and attempt to identify the loop conditions 
with comments: 
while ( $count < $max ) { 
// while $count is less than $max 
foreach ( $collection as $item ) { 
// until there are no more $items in the $collection 
• Save your file, then git add, git commit -m "explain why" and git push

More Related Content

What's hot

java collections
java collectionsjava collections
java collectionsjaveed_mhd
 
Advance java session 14
Advance java session 14Advance java session 14
Advance java session 14Smita B Kumar
 
Java Tutorial Lab 6
Java Tutorial Lab 6Java Tutorial Lab 6
Java Tutorial Lab 6Berk Soysal
 
Java Collections
Java CollectionsJava Collections
Java Collectionsparag
 
Java - Collections framework
Java - Collections frameworkJava - Collections framework
Java - Collections frameworkRiccardo Cardin
 
Ds session 8
Ds   session 8Ds   session 8
Ds session 8prags_nie
 
Collections in Java
Collections in JavaCollections in Java
Collections in JavaKhasim Cise
 
RECURSIVE DESCENT PARSING
RECURSIVE DESCENT PARSINGRECURSIVE DESCENT PARSING
RECURSIVE DESCENT PARSINGJothi Lakshmi
 
Java 103 intro to java data structures
Java 103   intro to java data structuresJava 103   intro to java data structures
Java 103 intro to java data structuresagorolabs
 
Collection Framework in java
Collection Framework in javaCollection Framework in java
Collection Framework in javaCPD INDIA
 
Java collections
Java collectionsJava collections
Java collectionsAmar Kutwal
 
Java collections concept
Java collections conceptJava collections concept
Java collections conceptkumar gaurav
 

What's hot (20)

java collections
java collectionsjava collections
java collections
 
Java util
Java utilJava util
Java util
 
Collections framework in java
Collections framework in javaCollections framework in java
Collections framework in java
 
Advance java session 14
Advance java session 14Advance java session 14
Advance java session 14
 
Java Tutorial Lab 6
Java Tutorial Lab 6Java Tutorial Lab 6
Java Tutorial Lab 6
 
Java Collections
Java CollectionsJava Collections
Java Collections
 
07 java collection
07 java collection07 java collection
07 java collection
 
Java Collections Framework
Java Collections FrameworkJava Collections Framework
Java Collections Framework
 
Java - Collections framework
Java - Collections frameworkJava - Collections framework
Java - Collections framework
 
Ds session 8
Ds   session 8Ds   session 8
Ds session 8
 
Java Collections Tutorials
Java Collections TutorialsJava Collections Tutorials
Java Collections Tutorials
 
Java 8 Features
Java 8 FeaturesJava 8 Features
Java 8 Features
 
Collections in Java
Collections in JavaCollections in Java
Collections in Java
 
RECURSIVE DESCENT PARSING
RECURSIVE DESCENT PARSINGRECURSIVE DESCENT PARSING
RECURSIVE DESCENT PARSING
 
Java 103 intro to java data structures
Java 103   intro to java data structuresJava 103   intro to java data structures
Java 103 intro to java data structures
 
Collection Framework in java
Collection Framework in javaCollection Framework in java
Collection Framework in java
 
Java collections
Java collectionsJava collections
Java collections
 
Java 8 Lambda and Streams
Java 8 Lambda and StreamsJava 8 Lambda and Streams
Java 8 Lambda and Streams
 
Java collections concept
Java collections conceptJava collections concept
Java collections concept
 
Array list(1)
Array list(1)Array list(1)
Array list(1)
 

Viewers also liked

Hooks, Actions, and Filters Oh My!
Hooks, Actions, and Filters Oh My!Hooks, Actions, and Filters Oh My!
Hooks, Actions, and Filters Oh My!David Wolfpaw
 
WordPress as a Minimum Viable Product - WordCamp Tampa 2014
WordPress as a Minimum Viable Product - WordCamp Tampa 2014WordPress as a Minimum Viable Product - WordCamp Tampa 2014
WordPress as a Minimum Viable Product - WordCamp Tampa 2014David Wolfpaw
 
Becoming a Respected WordPress Developer
Becoming a Respected WordPress DeveloperBecoming a Respected WordPress Developer
Becoming a Respected WordPress DeveloperDavid Wolfpaw
 
DIG1108C Lesson 2 Fall 2014
DIG1108C Lesson 2 Fall 2014DIG1108C Lesson 2 Fall 2014
DIG1108C Lesson 2 Fall 2014David Wolfpaw
 
DIG1108C Lesson 6 - Fall 2014
DIG1108C Lesson 6 - Fall 2014DIG1108C Lesson 6 - Fall 2014
DIG1108C Lesson 6 - Fall 2014David Wolfpaw
 
DIG1108C Lesson 7 Fall 2014
DIG1108C Lesson 7 Fall 2014DIG1108C Lesson 7 Fall 2014
DIG1108C Lesson 7 Fall 2014David Wolfpaw
 
DIG1108C Lesson 4 Fall 2014
DIG1108C Lesson 4 Fall 2014DIG1108C Lesson 4 Fall 2014
DIG1108C Lesson 4 Fall 2014David Wolfpaw
 
Dig1108C Lesson 1 Fall 2014
Dig1108C Lesson 1 Fall 2014Dig1108C Lesson 1 Fall 2014
Dig1108C Lesson 1 Fall 2014David Wolfpaw
 
Introduction to mobile usability
Introduction to mobile usabilityIntroduction to mobile usability
Introduction to mobile usabilityopendataottawa
 
WordPress tools and plugins
WordPress tools and pluginsWordPress tools and plugins
WordPress tools and pluginsDavid Wolfpaw
 
Spring Cleaning on Your Site
Spring Cleaning on Your SiteSpring Cleaning on Your Site
Spring Cleaning on Your SiteDavid Wolfpaw
 

Viewers also liked (12)

Hooks, Actions, and Filters Oh My!
Hooks, Actions, and Filters Oh My!Hooks, Actions, and Filters Oh My!
Hooks, Actions, and Filters Oh My!
 
WordPress as a Minimum Viable Product - WordCamp Tampa 2014
WordPress as a Minimum Viable Product - WordCamp Tampa 2014WordPress as a Minimum Viable Product - WordCamp Tampa 2014
WordPress as a Minimum Viable Product - WordCamp Tampa 2014
 
Becoming a Respected WordPress Developer
Becoming a Respected WordPress DeveloperBecoming a Respected WordPress Developer
Becoming a Respected WordPress Developer
 
DIG1108C Lesson 2 Fall 2014
DIG1108C Lesson 2 Fall 2014DIG1108C Lesson 2 Fall 2014
DIG1108C Lesson 2 Fall 2014
 
DIG1108C Lesson 6 - Fall 2014
DIG1108C Lesson 6 - Fall 2014DIG1108C Lesson 6 - Fall 2014
DIG1108C Lesson 6 - Fall 2014
 
DIG1108C Lesson 7 Fall 2014
DIG1108C Lesson 7 Fall 2014DIG1108C Lesson 7 Fall 2014
DIG1108C Lesson 7 Fall 2014
 
DIG1108C Lesson 4 Fall 2014
DIG1108C Lesson 4 Fall 2014DIG1108C Lesson 4 Fall 2014
DIG1108C Lesson 4 Fall 2014
 
Dig1108C Lesson 1 Fall 2014
Dig1108C Lesson 1 Fall 2014Dig1108C Lesson 1 Fall 2014
Dig1108C Lesson 1 Fall 2014
 
Oc bus tracker_120602
Oc bus tracker_120602Oc bus tracker_120602
Oc bus tracker_120602
 
Introduction to mobile usability
Introduction to mobile usabilityIntroduction to mobile usability
Introduction to mobile usability
 
WordPress tools and plugins
WordPress tools and pluginsWordPress tools and plugins
WordPress tools and plugins
 
Spring Cleaning on Your Site
Spring Cleaning on Your SiteSpring Cleaning on Your Site
Spring Cleaning on Your Site
 

Similar to Lists, Arrays, Loops and Conditionals in PHP Server-Side Programming

Similar to Lists, Arrays, Loops and Conditionals in PHP Server-Side Programming (20)

05php
05php05php
05php
 
ProgPrinc_Lecture_3_Data_Structures_and_Iteration-2.pdf
ProgPrinc_Lecture_3_Data_Structures_and_Iteration-2.pdfProgPrinc_Lecture_3_Data_Structures_and_Iteration-2.pdf
ProgPrinc_Lecture_3_Data_Structures_and_Iteration-2.pdf
 
rtwerewr
rtwerewrrtwerewr
rtwerewr
 
Php classes in mumbai
Php classes in mumbaiPhp classes in mumbai
Php classes in mumbai
 
Python Tutorial Part 1
Python Tutorial Part 1Python Tutorial Part 1
Python Tutorial Part 1
 
05php
05php05php
05php
 
MIND sweeping introduction to PHP
MIND sweeping introduction to PHPMIND sweeping introduction to PHP
MIND sweeping introduction to PHP
 
ProgFund_Lecture_3_Data_Structures_and_Iteration-1.pdf
ProgFund_Lecture_3_Data_Structures_and_Iteration-1.pdfProgFund_Lecture_3_Data_Structures_and_Iteration-1.pdf
ProgFund_Lecture_3_Data_Structures_and_Iteration-1.pdf
 
05php
05php05php
05php
 
php fundamental
php fundamentalphp fundamental
php fundamental
 
Php introduction with history of php
Php introduction with history of phpPhp introduction with history of php
Php introduction with history of php
 
php
phpphp
php
 
Matlab lec1
Matlab lec1Matlab lec1
Matlab lec1
 
Lecture 01 variables scripts and operations
Lecture 01   variables scripts and operationsLecture 01   variables scripts and operations
Lecture 01 variables scripts and operations
 
Array andfunction
Array andfunctionArray andfunction
Array andfunction
 
Programming Language
Programming  LanguageProgramming  Language
Programming Language
 
Introduction to Perl and BioPerl
Introduction to Perl and BioPerlIntroduction to Perl and BioPerl
Introduction to Perl and BioPerl
 
Php Introduction nikul
Php Introduction nikulPhp Introduction nikul
Php Introduction nikul
 
Python Programming and GIS
Python Programming and GISPython Programming and GIS
Python Programming and GIS
 
PHP - Introduction to PHP
PHP -  Introduction to PHPPHP -  Introduction to PHP
PHP - Introduction to PHP
 

More from David Wolfpaw

Running Your Service Business on WordPress
Running Your Service Business on WordPressRunning Your Service Business on WordPress
Running Your Service Business on WordPressDavid Wolfpaw
 
Stop the Green Light Panic - Lisa Melegari
Stop the Green Light Panic - Lisa MelegariStop the Green Light Panic - Lisa Melegari
Stop the Green Light Panic - Lisa MelegariDavid Wolfpaw
 
php[world] Hooks, Actions and Filters Oh My!
php[world] Hooks, Actions and Filters Oh My!php[world] Hooks, Actions and Filters Oh My!
php[world] Hooks, Actions and Filters Oh My!David Wolfpaw
 
DIG1108C Lesson3 Fall 2014
DIG1108C Lesson3 Fall 2014DIG1108C Lesson3 Fall 2014
DIG1108C Lesson3 Fall 2014David Wolfpaw
 
Beginner Workshop WCMIA
Beginner Workshop WCMIABeginner Workshop WCMIA
Beginner Workshop WCMIADavid Wolfpaw
 
Basic word press development
Basic word press developmentBasic word press development
Basic word press developmentDavid Wolfpaw
 
Geekaboo presentation 2013 - Brett Napoli
Geekaboo presentation 2013 - Brett NapoliGeekaboo presentation 2013 - Brett Napoli
Geekaboo presentation 2013 - Brett NapoliDavid Wolfpaw
 
Organization methods in word press
Organization methods in word pressOrganization methods in word press
Organization methods in word pressDavid Wolfpaw
 

More from David Wolfpaw (8)

Running Your Service Business on WordPress
Running Your Service Business on WordPressRunning Your Service Business on WordPress
Running Your Service Business on WordPress
 
Stop the Green Light Panic - Lisa Melegari
Stop the Green Light Panic - Lisa MelegariStop the Green Light Panic - Lisa Melegari
Stop the Green Light Panic - Lisa Melegari
 
php[world] Hooks, Actions and Filters Oh My!
php[world] Hooks, Actions and Filters Oh My!php[world] Hooks, Actions and Filters Oh My!
php[world] Hooks, Actions and Filters Oh My!
 
DIG1108C Lesson3 Fall 2014
DIG1108C Lesson3 Fall 2014DIG1108C Lesson3 Fall 2014
DIG1108C Lesson3 Fall 2014
 
Beginner Workshop WCMIA
Beginner Workshop WCMIABeginner Workshop WCMIA
Beginner Workshop WCMIA
 
Basic word press development
Basic word press developmentBasic word press development
Basic word press development
 
Geekaboo presentation 2013 - Brett Napoli
Geekaboo presentation 2013 - Brett NapoliGeekaboo presentation 2013 - Brett Napoli
Geekaboo presentation 2013 - Brett Napoli
 
Organization methods in word press
Organization methods in word pressOrganization methods in word press
Organization methods in word press
 

Lists, Arrays, Loops and Conditionals in PHP Server-Side Programming

  • 1. Intro to Server Side Programming Lesson Five
  • 2. Lists and Dictionaries • In computer science, a list (or sequence) is an abstract data type that implements an ordered collection of values. Each instance of a value on a list is usually called an item, entry or element of the list, and if the same value appears multiple times in the list, each is considered a distinct item of the list. • An associative array (or dictionary) is an abstract data type composed of a collection of key/value pairs, such that each key appears at most once in the collection. Pairs can be added or removed, and values can be modified or retrieved based on the associated key.
  • 3. What's The Difference? • A list is always ordered, can usually return elements or items by numeric index(starting at zero), and might not allow modification of values. A decent implementation of a list should provide specific operations for adding and removing elements to enable them to be used: • push - append to the end(or tail) of the list • pop - remove from the tail of the list • queue(unshift) - append to the beginning(or head) of the list • dequeue(shift) - remove from the head of the list • A hash is usually not immutable and may be indeterminately ordered. Values can usually be altered.
  • 4. Arrays • An array type is a data type that is meant to describe a collection of values or variables, each selected by one or more indices(keys) that can be computed at run-time of the program. • By default, the array type in PHP generates computed, numerical indices, starting with zero, to make a list: var_dump(array( 'one', 'two', 3, 4, 4.1, 4.2 )); $list = array(); $list[] = 'one'; $list[] = 'two'; • The value for the key can also be specified as any scalar literal (neither array, object nor resource), often a string var_dump(array( 'one' => 1, 2 => 'two' )): $list[4] = 'four'; $list['five dot one'] = 5.1;
  • 5. Array Tricks • PHP relies heavily on arrays, so there are a lot of array-specific functions, like to use an array as a queue or stack • array_unshift(), array_shift() - append to and remove from head • array_push(), array_pop() - append to and remove from tail • PHP arrays have an "internal position" pointer that can be used as an iterator: • current(), key() - return the current element or key • next(), prev(), end(), reset() - advance forward or back • each() - return the current key and value and advance the pointer
  • 6. Even More Array Tricks • array_merge() - merge two or more arrays • array_slice() - remove an array section • array_splice() - insert or replace a section • count(), sizeof() - calculate length • array_fill(), array_pad(), range() - generate values • str_split(), explode(), implode(), join() - array to string
  • 7. Assignment 5.1 Find Some Lists and Arrays
  • 8. Lists of Lists • Open Github • Look through your forked projects for examples of lists and dictionary definitions and methods • Copy and paste your examples into a file named "homework-5.1.md" with code delimiters as needed • Write comments identifying the lists, keys, indexes and other list and dictionary pieces discussed • Save, add and commit, then push to Github when done
  • 10. Control Flow Statements • Control Flow - refers to the order in which the individual statements, instructions or function calls of an imperative or declarative program are executed or evaluated. Execution results in a choice being made as to which of two or more paths should be followed. • Types of Control Flow statements: • continuation at a different statement (unconditional branch or jump) • execute statements only if some condition is met (conditional branch) • execute statements until some conditional is met(loop, conditional branch) • execute defined statements and return (sub/co-routines, continuations)s • stop executing statements (unconditional halt)
  • 11. Loops • A loop is a sequence of statements which is specified once but which may be carried out several times in succession, a specified number of times or indefinitely • Specific number of times: for ( $count = 0; $count < $max; $count++ ) do_something(); • Once per each item in a collection(array): foreach ( $collection as $item ) do_something(); foreach ($collection as $key => $value ) do_something(); • Until some condition is met: while ( $condition == true ) do_something(); do something(); while ( $condition ); • Indefinitely(infinitely): while ( true ) do_something(); do something(); while ( true);
  • 12. Infinite Loops • An unconditional(or infinite) loop returns to a fixed point in the diagram, usually the top of the workflow. Without a breaking statement or escape clause, it executes forever.
  • 13. Conditional Loops • A conditional loop (while, do-while, for, foreach) returns to a condition check ($count < $max) Until the condition evaluates FALSE, the loop will continue to execute
  • 14. Diagramming Part Two • In your pair, find a section of a project that has some significant looping and conditional logic (at least three branches) • Individually, sketch a simple workflow diagram of the logic; assemble a truth table if needed • Discuss differences in your diagrams and make a new diagram and truth table for the logic to demonstrate
  • 15. Assignment 5.2 Loops and Conditionals
  • 16. • Create a file called "homework-5.2.md" in your “assignments" folder • Find a section in your projects that has some decent looping and branching code: at least five branches that you can diagram. • Copy and paste your example into your file and attempt to identify the loop conditions with comments: while ( $count < $max ) { // while $count is less than $max foreach ( $collection as $item ) { // until there are no more $items in the $collection • Save your file, then git add, git commit -m "explain why" and git push