Arrays Strings and regular expressions Basic PHP Syntax CS380 1.

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

Intro to Javascript CS Client Side Scripting CS380 2.
NETLOGO LISTS Or… James says, fput THIS!. What are LISTS? Shopping list Address list DNA sequences Any collection of similar or dissimilar things Often.
Intro to Scala Lists. Scala Lists are always immutable. This means that a list in Scala, once created, will remain the same.
ArrayLists The lazy mans array. Whats the matter here? int[] list = new int[10]; list[0] = 5; list[2] = hey; list[3] = 15; list[4] = 23;
Chapter 6 Lists and Dictionaries CSC1310 Fall 2009.
Arrays. What is an array An array is used to store a collection of data It is a collection of variables of the same type.
CHAPTER 7 Queues.
String and Lists Dr. Benito Mendoza. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list List.
Dynamic Arrays Lecture 4. Arrays In many languages the size of the array is fixed however in perl an array is considered to be dynamic: its size can be.
Lecture 12 – ADTs and Stacks.  Modularity  Divide the program into smaller parts  Advantages  Keeps the complexity managable  Isolates errors (parts.
CS 206 Introduction to Computer Science II 03 / 06 / 2009 Instructor: Michael Eckmann.
Programming with Collections Collections in Java Using Arrays Week 9.
CS 206 Introduction to Computer Science II 10 / 17 / 2008 Instructor: Michael Eckmann.
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.
JAVA 1.5 New Features Dr V. The syntax of the enhanced for loop (for each) for (type variableName : arrayName) { statements } arrayName could be any.
CS 206 Introduction to Computer Science II 10 / 28 / 2009 Instructor: Michael Eckmann.
Lists, Stacks, Queues Svetlin Nakov Telerik Corporation
CSE 341 Lecture 24 JavaScript arrays and objects slides created by Marty Stepp
More on PHP: Arrays, Functions and Files
CSE 154 LECTURE 6: EMBEDDED PHP. PHP syntax template HTML content HTML content HTML content... PHP any contents of.
COMPUTER SCIENCE FEBRUARY 2011 Lists in Python. Introduction to Lists Lists (aka arrays): an ordered set of elements  A compound data type, like strings.
Recursion Examples Fundamentals of CS Case 1: Code /* Recursion: Case 1 */ #include void count (int index); main () { count (0); getchar(); } void count.
Week7 Stack Data Structures & Algorithms. Introduction to Stacks and Queues Widely used data structures Ordered List of element Easy to implement Easy.
Containers Overview and Class Vector
Data Structures in Python By: Christopher Todd. Lists in Python A list is a group of comma-separated values between square brackets. A list is a group.
AP Comp Sci A Chapter 12 - Arrays. Ch 12 Goals: Goals: Declare and create arrays Declare and create arrays Access elements in arrays Access elements in.
Array & Foreach อาร์เรย์และคำสั่งวนลูป. Content 1. Definition and Usage 2. Syntax 3. print_r() Statement 4. For and Foreach 5. Array Functions.
CSE 373: Data Structures and Algorithms Lecture 1: Introduction; ADTs; Stacks; Eclipse.
1 PHP Intro PHP Arrays After this lecture, you should be able to: Create and manipulate PHP Arrays: Create and manipulate PHP Arrays: Indexed Arrays Indexed.
Built-in Data Structures in Python An Introduction.
Slide 1 PHP Arrays and User Defined Functions ITWA133.
Python Arrays. An array is a variable that stores a collection of things, like a list. For example a list of peoples names. We can access the different.
Arrays and Collections Tonga Institute of Higher Education.
Project 1: Using Arrays and Manipulating Strings Essentials for Design JavaScript Level Two Michael Brooks.
10 – Java Script (3) Informatics Department Parahyangan Catholic University.
Data Types 2 Arithmetic funtions ans laws.
Chemistry CS1020.  Given a set of mappings and chemical formula: Calculate the molecule mass of the formula Problem Description C12 H1 N14 O16 (NH 4.
Perl Variables: Array Web Programming1. Review: Perl Variables Scalar ► e.g. $var1 = “Mary”; $var2= 1; ► holds number, character, string Array ► e.g.
1 BUILDING JAVA PROGRAMS CHAPTER 7.2 ARRAY TRAVERSAL ALGORITHMS.
C# E1 CSC 298 Arrays in C#. C# E2 1D arrays  A collection of objects of the same type  array of integers of size 10 int[] a = new int[10];  The size.
LECTURE 3 Python Basics Part 2. FUNCTIONAL PROGRAMMING TOOLS Last time, we covered function concepts in depth. We also mentioned that Python allows for.
 An array stores multiple values in one single variable.  Example: Output: I like Honda Civic, BMW and Toyota.
ECA 225 Applied Interactive Programming ECA 225 Applied Online Programming javascript arrays.
Arrays and Lists. What is an Array? Arrays are linear data structures whose elements are referenced with subscripts. Just about all programming languages.
Class02 More Arrays MIS 3502, Spring 2016 Jeremy Shafer Department of MIS Fox School of Business Temple University 1/14/2016.
Stacks. What is a Stack? A stack is a type of data structure (a way of organizing and sorting data so that it can be used efficiently). To be specific,
CSC 4630 Perl 2 adapted from R. E. Beck. I/O and Arithmetic Form pairs of programmer/investigator/discover Exercise 1. Write a program that prompts the.
Arrays Strings and regular expressions Basic PHP Syntax CS380 1.
Click to edit Master text styles Stacks Data Structure.
1 PHP Array PHP Arrays An indexed array is similar to one provided by a conventional programming language. An indexed array is similar to one provided.
CSE 154 LECTURE 15: EMBEDDED PHP. PHP syntax template HTML content HTML content HTML content... PHP any contents of.
String and Lists Dr. José M. Reyes Álamo. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list.
Introduction to Programming the WWW I CMSC Winter 2004 Lecture 8.
String and Lists Dr. José M. Reyes Álamo.
Building Java Programs
Class06 Arrays MIS 3502 Jeremy Shafer Department of MIS
PART II STACK APPLICATIONS
Road Map CS Concepts Data Structures Java Language Java Collections
Arrays MIS 3502 Jeremy Shafer Department of MIS Fox School of Business
Data Structures – 1D Lists
Arrays .
String and Lists Dr. José M. Reyes Álamo.
Chapter 5: Lists and Dictionaries
Welcome to CSE 143! Go to pollev.com/cse143.
CS2011 Introduction to Programming I Arrays (I)
Fundaments of Game Design
CSE 154 Lecture 15: MORE PHP.
Python List.
Tuple.
Presentation transcript:

Arrays Strings and regular expressions Basic PHP Syntax CS380 1

Arrays 2  Append: use bracket notation without specifying an index  Element type is not specified; can mix types $name = array(); # create $name = array(value0, value1,..., valueN); $name[index] # get element value $name[index] = value; # set element value $name[] = value; # append PHP CS380 $a = array(); # empty array (length 0) $a[0] = 23; # stores 23 at index 0 (length 1) $a2 = array("some", "strings", "in", "an", "array"); $a2[] = "Ooh!"; # add string to end (at index 5) PHP

Array functions 3

Array function example 4  the array in PHP replaces many other collections in Java  list, stack, queue, set, map,... $tas = array("MD", "BH", "KK", "HM", "JP"); for ($i = 0; $i < count($tas); $i++) { $tas[$i] = strtolower($tas[$i]); } $morgan = array_shift($tas); array_pop($tas); array_push($tas, "ms"); array_reverse($tas); sort($tas); $best = array_slice($tas, 1, 2); PHP CS380

foreach loop 5 foreach ($array as $variableName) {... } PHP CS380 $fellowship = array(“Frodo", “Sam", “Gandalf", “Strider", “Gimli", “Legolas", “Boromir"); print “The fellowship of the ring members are: \n"; for ($i = 0; $i < count($fellowship); $i++) { print "{$fellowship[$i]}\n"; } print “The fellowship of the ring members are: \n"; foreach ($fellowship as $fellow) { print "$fellow\n"; } PHP

Multidimensional Arrays 6 <?php $AmazonProducts = array( array(“BOOK", "Books", 50), array("DVDs", “Movies", 15), array(“CDs", “Music", 20) ); for ($row = 0; $row < 3; $row++) { for ($column = 0; $column | PHP CS380

Multidimensional Arrays (cont.) 7 “BOOK", “Description” => "Books", “Price” => 50), array(“Code” => "DVDs", “Description” => “Movies", “Price” => 15), array(“Code” => “CDs", “Description” => “Music", “Price” => 20) ); for ($row = 0; $row | | | PHP CS380

String compare functions 8  Comparison can be:  Partial matches  Others  Variations with non case sensitive functions  strcasecmp strcasecmp

String compare functions examples 9 $offensive = array( offensive word1, offensive word2); $feedback = str_replace($offcolor, $feedback); PHP CS380 $test = “Hello World! \n”; print strpos($test, “o”); print strpos($test, “o”, 5); PHP $toaddress = if(strstr($feedback, “shop”) $toaddress = else if(strstr($feedback, “delivery”) $toaddress = PHP

Regular expressions 10 [a-z]at #cat, rat, bat… [aeiou] [a-zA-Z] [^a-z] #not a-z [[:alnum:]]+ #at least one alphanumeric char (very) *large #large, very very very large… (very){1, 3} #counting “very” up to 3 ^bob #bob at the beginning com$ #com at the end PHPRegExp  Regular expression: a pattern in a piece of text  PHP has:  POSIX  Perl regular expressions CS380

Embedded PHP 11 CS380

Printing HTML tags in PHP = bad style 12 <?php print "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\"\n"; print " \" print " \n"; print " Geneva's web page \n";... for ($i = 1; $i <= 10; $i++) { print " I can count to $i! \n"; } ?> HTML  best PHP style is to minimize print/echo statements in embedded PHP code  but without print, how do we insert dynamic content into the page?

PHP expression blocks 13  PHP expression block: a small piece of PHP that evaluates and embeds an expression's value into HTML  is equivalent to: PHP CS380 The answer is PHP The answer is 42 output PHP

Expression block example 14 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" " CSE 190 M: Embedded PHP <?php for ($i = 99; $i >= 1; $i--) { ?> bottles of beer on the wall, bottles of beer. Take one down, pass it around, bottles of beer on the wall. <?php } ?> PHP

Common errors: unclosed braces, missing = sign Watch how high I can count: <?php for ($i = 1; $i <= 10; $i++) { ?> PHP  if you forget to close your braces, you'll see an error about 'unexpected $end '  if you forget = in <?=, the expression does not produce any output CS380

Complex expression blocks <?php for ($i = 1; $i <= 3; $i++) { ?> >This is a level heading. > <?php } ?> PHP CS380 This is a level 1 heading. This is a level 2 heading. This is a level 3 heading. output

Functions Advanced PHP Syntax 17 CS380

Functions 18 function name(parameterName,..., parameterName) { statements; } PHP CS380 function quadratic($a, $b, $c) { return -$b + sqrt($b * $b - 4 * $a * $c) / (2 * $a); } PHP  parameter types and return types are not written  a function with no return statements implicitly returns NULL

Default Parameter Values 19 function print_separated($str, $separator = ", ") { if (strlen($str) > 0) { print $str[0]; for ($i = 1; $i < strlen($str); $i++) { print $separator. $str[$i]; } } PHP CS380 print_separated("hello"); # h, e, l, l, o print_separated("hello", "-"); # h-e-l-l-o PHP  if no value is passed, the default will be used

PHP Arrays Ex. 1  Arrays allow you to assign multiple values to one variable. For this PHP exercise, write an array variable of weather conditions with the following values: rain, sunshine, clouds, hail, sleet, snow, wind. Using the array variable for all the weather conditions, echo the following statement to the browser: We've seen all kinds of weather this month. At the beginning of the month, we had snow and wind. Then came sunshine with a few clouds and some rain. At least we didn't get any hail or sleet.  Don't forget to include a title for your page, both in the header and on the page itself. CS380 20

PHP Arrays Ex. 2  For this exercise, you will use a list of ten of the largest cities in the world. (Please note, these are not the ten largest, just a selection of ten from the largest cities.) Create an array with the following values: Tokyo, Mexico City, New York City, Mumbai, Seoul, Shanghai, Lagos, Buenos Aires, Cairo, London.  Print these values to the browser separated by commas, using a loop to iterate over the array. Sort the array, then print the values to the browser in an unordered list, again using a loop.  Add the following cities to the array: Los Angeles, Calcutta, Osaka, Beijing. Sort the array again, and print it once more to the browser in an unordered list. CS380 21