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

Slides:



Advertisements
Similar presentations
Character Arrays (Single-Dimensional Arrays) A char data type is needed to hold a single character. To store a string we have to use a single-dimensional.
Advertisements

Chapter 4: Section 6 Compound Probability.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 7.9Arrays of Pointers Arrays can contain pointers For.
 2000 Prentice Hall, Inc. All rights reserved Fundamentals of Strings and Characters String declarations –Declare as a character array or a variable.
1 Various Methods of Populating Arrays Randomly generated integers.
Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, © Copyright 2010 All Rights Reserved. 1.
Functions & Arrays. Functions Functions offer the ability for programmers to group together program code that performs specific task or function into.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 7 - Pointers Outline 7.1Introduction 7.2Pointer.
C SCI 116.  What does $animals contain? 2 $animals = "ostrich"; $animals = "anteater"; $animals = "orangutan"; $animals = "cheetah"; $animals = "hyena";
Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, © Copyright 2010, All Rights Reserved 1.
Lesson 6 - Pointers Outline Introduction Pointer Variable Declarations and Initialization Pointer Operators Calling Functions by Reference Using the const.
Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, © Copyright 2010 All Rights Reserved. 1.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 7 - Pointers Outline 7.1Introduction 7.2Pointer Variable Declarations and Initialization 7.3Pointer.
Lecture 2 BNFO 240. Perl online references
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.
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.
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.
Welcome to Survey of Mathematics Unit 7
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.
 2004 Prentice Hall, Inc. All rights reserved. Chapter 25 – Perl and CGI (Common Gateway Interface) Outline 25.1 Introduction 25.2 Perl 25.3 String Processing.
1 CS428 Web Engineering Lecture 19 Data Types (PHP - II)
PHP – Get & Post; Functions; and Arrays IS6116 – 07 th February 2011.
DAY 4. Review: Strings Strings can be concatenated (joined together) using the. (period) operator: $fullName = “Thomas ”. “Bombach”; This works when outputting.
Strings and Arrays. String Is a sequence of characters. Example: “hello”, “how are you?”, “123”,and are all valid string values.
Lecture 6 – Form processing (Part 1) SFDV3011 – Advanced Web Development 1.
CSE 154 LECTURE 6: EMBEDDED PHP. PHP syntax template HTML content HTML content HTML content... PHP any contents of.
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.”
Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, Copyright © 2015, Fred McClurg, All Rights.
Compound Probability Pre-AP Geometry. Compound Events are made up of two or more simple events. I. Compound Events may be: A) Independent events - when.
Arrays By Shyam Gurram. What is an Array? An array can store one or more values in a single variable name. Each element in the array is assigned its own.
Web Scripting [PHP] CIS166AE Wednesdays 6:00pm – 9:50pm Rob Loy.
Chapter 7 Arrays. A 12-element array Declaring and Creating Arrays Arrays are objects that occupy memory Created dynamically with keyword new int c[]
Introduction to PHP – Part 2 Sudeshna Dey. Arrays A series of homogeneous elements Elements have values in form of stored data Has a key associated with.
HTML Table and PHP Array
Array & Foreach อาร์เรย์และคำสั่งวนลูป. Content 1. Definition and Usage 2. Syntax 3. print_r() Statement 4. For and Foreach 5. Array Functions.
Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, Copyright © 2015, Fred McClurg, All Rights.
Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, Copyright © 2010 All Rights Reserved.
Copyright 2008 by Pearson Education Building Java Programs ArrayList Reading: 10.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 2015, Fred McClurg, All Rights.
Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, © Copyright 2015, Fred McClurg, All Rights.
C Lecture Notes 1 Structures & Unions. C Lecture Notes Introduction Structures –Collections of related variables (aggregates) under one name Can.
Copyright 2010 by Pearson Education Building Java Programs Chapter 10, 11 Lecture 22: 143 Preview optional reading: 10.1,
1 Lecture 12 Pointers and Strings Section 5.4, ,
1 Lecture 8 Pointers and Strings: Part 2 Section 5.4, ,
Algebra-2 Counting and Probability. Quiz 10-1, Which of these are an example of a “descrete” set of data? 2.Make a “tree diagram” showing all.
0-11 Probability Goal: Find the probability of an event occurring. Eligible Content: A
NMD202 Web Scripting Week2. Web site
ECA 225 Applied Interactive Programming ECA 225 Applied Online Programming javascript arrays.
Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, © Copyright 2015, Fred McClurg, All Rights.
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.
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.
CSE 154 LECTURE 15: EMBEDDED PHP. PHP syntax template HTML content HTML content HTML content... PHP any contents of.
By Joseph Patrick Brady. Overview  Hashes  foreach control statement  split function.
Arrays: Checkboxes and Textareas
Kirkwood Center for Continuing Education
Building Java Programs
Kirkwood Center for Continuing Education
The meaning of probability
String functions
Presentation transcript:

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

Chapter Six Hurray for Array Functions hp/slides/chapter06e.functions.ppt 2

Description: Adds one or more elements to the end of an array. Syntax: array_push( &$array, $elements ); Example:<?php $obj = array( "rock" ); $obj = array( "rock" ); array_push( $obj, "paper", array_push( $obj, "paper", "scissors" ); "scissors" ); for ( $i = 0; $i < count($obj); $i++ ) for ( $i = 0; $i < count($obj); $i++ ) printf( "\$obj[%d]: %s ", printf( "\$obj[%d]: %s ", $i, $obj[$i] ); $i, $obj[$i] );?> Array Function: “array_push” 3 What is contained in array “$obj”?

Description: Removes the last element of an array and returns that value. Syntax: $last = array_pop( &$array ); Example: <?php $stooge = array( "Moe", "Larry", "Curly" ); $best = array_pop( $stooge ); for ( $i = 0; $i < count($stooge); $i++ ) printf( "\$stooge[%d]: %s ", $i, $stooge[$i] ); printf( " " ); printf( "\$best: %s", $best ); ?> Array Function: “array_pop” 4 What is contained in array “$stooge”?

Description: Adds one or more elements to the beginning of an array. Syntax: array_unshift( &$array, $elements ); Example:<?php $stooge = array( "Moe", "Larry" ); $stooge = array( "Moe", "Larry" ); array_unshift( $stooge, "Shemp" ); array_unshift( $stooge, "Shemp" ); for ( $i = 0; $i < count($stooge); $i++ ) for ( $i = 0; $i < count($stooge); $i++ ) printf( "\$stooge[%d]: %s ", printf( "\$stooge[%d]: %s ", $i, $stooge[$i] ); $i, $stooge[$i] );?> Array Function: “array_unshift” 5 What is the contents of array “$stooge”?

Description: Removes the first element of an array and returns that value. Syntax: $first = array_shift( &$array ); Example: <?php $narnia = array( "Peter", "Susan", "Lucy", "Edmund" ); $highKing = array_shift( $narnia ); for ( $i = 0; $i < count($narnia); $i++ ) printf( "\$narnia[%d]: %s ", $i, $narnia[$i] ); printf( "\$highKing: %s", $highKing ); ?> Array Function: “array_shift” 6 What is the contents of array “$naria”?

Description: Arranges array values in random order. Syntax: shuffle( &$array ); Example: <?php $deck = array( "Ace", "King", "Queen", "Jack", "Joker" ); shuffle( $deck ); // randomize array for ( $i = 0; $i < count($deck); $i++ ) printf( "\$deck[%d]: %s ", $i, $deck[$i] ); ?> Array Function: “shuffle” 7 What is the value of array “$deck”?

Description: Sorts the array in alphabetical order. Syntax: sort( &$array ); Example: <?php $torah = array( "Genesis", "Exodus", "Leviticus", "Numbers", "Deuteronomy" ); sort( $torah ); // sort array by value for ( $i = 0; $i < count($torah); $i++ ) printf( "\$torah[%d]: %s ", $i, $torah[$i] ); ?> Array Function: “sort” 8 What is the value of array “$torah”?

Description: Reverses the order of the array values. Syntax: $arrayNew = array_reverse( $arrayOld ); Example: <?php $name = "rumpelstiltskin"; // convert string to array $pieces = str_split( $name ); // reverse the array order $reverse = array_reverse( $pieces ); // convert array back to string $backward = implode( "", $reverse ); echo $backward; ?> Array Function: “array_reverse” 9 What is the value of variable “$backward”?

Description: Removes a portion of an array and replace it with something else. Syntax: $aryRm = array_splice( &$input, $offset[, $length=0 [, $replacement]] ); Example: <?php $deck = array( "Ace", "King", "Queen", "Jack", "Joker" ); $max = count($deck) - 1; $offset = rand( 0, $max ); // draw one random card $drawn = array_splice( $deck, $offset, 1 ); printf( "Card drawn: %s ", $drawn[0] ); printf( "Remaining deck: " ); for ( $i = 0; $i < count($deck); $i++ ) printf( "\$deck[%d]: %s ", $i, $deck[$i] ); ?> Array Function: “array_splice” 10 What are the values in array “$deck”?

array_walk(): Send value/key pairs of an associative array to be handled by a user defined function. User Defined Function Syntax: <?php function functName( $value, $key ) function functName( $value, $key ) { } array_walk( $hash, 'functName' ); array_walk( $hash, 'functName' );?> Don’t get angry, just walk array! 11

<?php function printRow( $value, $key ) function printRow( $value, $key ) { printf( " printf( " $key $key $value $value \n" ); \n" ); } $color = array( 'Red' => '#FF0000', $color = array( 'Red' => '#FF0000', 'Green' => '#00FF00', 'Green' => '#00FF00', 'Blue' => '#0000FF', 'Blue' => '#0000FF', 'Yellow' => '#FFFF00' ); 'Yellow' => '#FFFF00' ); array_walk( $color, 'printRow' ); array_walk( $color, 'printRow' );?> “array_walk” Example 12 What is printed to the screen?

Problem: Write a string scramble simulation game. The order of the characters shall be randomized. Input: Initialize a variable to a word or string to be scrambled. Output: Display the original string and the randomized string to the screen. Hint One: Use the predefined functions, “str_split()”, “shuffle()” and “implode()” to make the task easier. Hint Two: See example for “array_reverse()”. Student Exercise 6.3 Array Functions 13 13

<?php $word = "randomize"; // break each char into array $pieces = str_split( $word ); // randomize the array shuffle( $pieces ); // convert elements into string $scramble = implode( "", $pieces ); printf( "Original: %s Scrambled: %s ", $word, $scramble ); ?> Student Solution 6.3 Student Solution 6.3 Array Functions 14 What does the string “$scramble” contain?

Problem: Write a sentence scramble simulation game. The order of the words shall be randomized. The words themselves shall not be scrambled. Input: Initialize a variable to number of words or a sentence to be scrambled. Output: Display the original sentence and the randomized sentence to the screen. Hint One: Solution will be similar to Exercise 6.2 Hint Two: Use predefined function “explode” instead of “str_split”. Student Exercise 6.4 Array Functions 15

<?php $sentence = "Now is the time for all good men to pray for the aid of their country."; $sentence = "Now is the time for all good men to pray for the aid of their country."; // at a space, break each word into an array // at a space, break each word into an array $words = explode( " ", $sentence ); $words = explode( " ", $sentence ); // randomize the array // randomize the array shuffle( $words ); shuffle( $words ); // join elements into string with spaces // join elements into string with spaces $scramble = implode( " ", $words ); $scramble = implode( " ", $words ); printf( "Original: %s printf( "Original: %s Scrambled: %s", $sentence, $scramble ); Scrambled: %s", $sentence, $scramble );?> Student Solution 6.4 Array Functions 16

to be continued... p/slides/chapter08a.fundamentals.ppt p/slides/chapter08a.fundamentals.ppt17