LIS651 lecture 3 functions and arrays Thomas Krichel 2010-02-11.

Slides:



Advertisements
Similar presentations
LIS651 lecture 3 taming PHP Thomas Krichel
Advertisements

LIS651 lecture 3 taming PHP Thomas Krichel
LIS651 lecture 1 PHP basics, database introduction Thomas Krichel
LIS651 lecture 1 arrays functions & sessions Thomas Krichel
LIS651 lecture 0 Gathering and showing data Thomas Krichel
LIS651 lecture 3 numbers, Boolean, control flow Thomas Krichel
LIS651 lecture 3 functions & sessions Thomas Krichel
Arrays A list is an ordered collection of scalars. An array is a variable that holds a list. Arrays have a minimum size of 0 and a very large maximum size.
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.
This Time Whitespace and Input/Output revisited The Programming cycle Boolean Operators The “if” control structure LAB –Write a program that takes an integer.
Python Basics: Statements Expressions Loops Strings Functions.
 Variables  What are they?  Declaring and initializing variables  Common uses for variables  Variables you get “for free” in Processing ▪ Aka: Built-in.
BBS514 Structured Programming (Yapısal Programlama)1 Functions and Structured Programming.
Perl Arrays and Lists Learning Objectives: 1. To understand the format and the declaration of Arrays & Lists in Perl 2. To distinguish the difference between.
176 Formal Languages and Applications: We know that Pascal programming language is defined in terms of a CFG. All the other programming languages are context-free.
Programming with Collections Collections in Java Using Arrays Week 9.
Arrays  Writing a program that uses a large amount of information.  Such as a list of 100 elements.  It is not practical to declare.
CS 106 Introduction to Computer Science I 02 / 22 / 2008 Instructor: Michael Eckmann.
ECE122 L11: For loops and Arrays March 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 11 For Loops and Arrays.
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays –Structures of related data items –Static entity (same size throughout program) A few types –Pointer-based.
CS 106 Introduction to Computer Science I 10 / 09 / 2006 Instructor: Michael Eckmann.
PHYS 2020 Making Choices; Arrays. Arrays  An array is very much like a matrix.  In the C language, an array is a collection of variables, all of the.
Chapter 9: Arrays and Strings
CS 106 Introduction to Computer Science I 02 / 19 / 2007 Instructor: Michael Eckmann.
PHP Server-side Programming. PHP  PHP stands for PHP: Hypertext Preprocessor  PHP is interpreted  PHP code is embedded into HTML code  interpreter.
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.
LIS651 lecture 1 PHP basics Thomas Krichel
Forth A stack language.
Lists in Python.
FUNCTIONS AND STORED PROCEDURES & FUNCTIONS AND PROTECTING A DB AND PHP (Chapters 9, 15, 18)
CS0004: Introduction to Programming Variables – Strings.
PHP Controlling Program Flow Mohammed M. Hassoun 2012.
C++ Basics Structure of a Program. C++ Source Code Plain text file Typical file extension .CPP Must compile the C++ source code without errors before.
Scripting Languages Diana Trandab ă ț Master in Computational Linguistics - 1 st year
Session 7 JavaScript/Jscript: Arrays Matakuliah: M0114/Web Based Programming Tahun: 2005 Versi: 5.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Arrays.
Web Database Programming Week 3 PHP (2). Functions Group related statements together to serve “a” specific purpose –Avoid duplicated code –Easy maintenance.
Class 2Intro to Databases Goals of this class Include & Require in PHP Generating Random Numbers in PHP Arrays – Numerically Indexed and Associative Program.
CS 330 Programming Languages 10 / 07 / 2008 Instructor: Michael Eckmann.
Built-in Data Structures in Python An Introduction.
_______________________________________________________________________________________________________________ PHP Bible, 2 nd Edition1  Wiley and the.
Introducing Python CS 4320, SPRING Lexical Structure Two aspects of Python syntax may be challenging to Java programmers Indenting ◦Indenting is.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 3 Mathematical Functions, Strings, and Objects.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
1 Character Strings (Cstrings) Reference: CS215 textbook pages
1 One Dimensional Arrays Chapter 11 2 "All students to receive arrays!" reports Dr. Austin. Declaring arrays scores :
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Operating System Discussion Section. The Basics of C Reference: Lecture note 2 and 3 notes.html.
29 January 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
ITM © Port, KazmanVariables - 1 ITM 352 Data types, Variables Class #4.
Programming Fundamentals. Today’s Lecture Array Fundamentals Arrays as Class Member Data Arrays of Objects C-Strings The Standard C++ string Class.
Form Data (part 2) MIS 3502, Fall 2015 Brad N Greenwood, PhD Department of MIS Fox School of Business Temple University 11/10/2015 Slide 1.
1 Lecture 4: Part1 Arrays Introduction Arrays  Structures of related data items  Static entity (same size throughout program)
Radoslav Georgiev Telerik Corporation
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
© 2004 Pearson Addison-Wesley. All rights reserved October 5, 2007 Arrays ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor:
Form Data (part 2) MIS 3502, Fall 2015 Jeremy Shafer Department of MIS
Loops BIS1523 – Lecture 10.
JavaScript: Functions.
In Class Program: Today in History
Arrays We often want to organize objects or primitive data in a way that makes them easy to access and change. An array is simple but powerful way to.
Perl I/O Learning Objectives:
Text Analyzer BIS1523 – Lecture 14.
Chapter 3 Mathematical Functions, Strings, and Objects
Appendix B.1 Lex Appendix B.1 -- Lex.
Arrays Arrays A few types Structures of related data items
COMPUTER PROGRAMMING SKILLS
Class code for pythonroom.com cchsp2cs
Lex Appendix B.1 -- Lex.
Presentation transcript:

LIS651 lecture 3 functions and arrays Thomas Krichel

today more string functions general function desciption more number functions arrays the foreach() {} statement

string functions There are a long list of string functions in the PHP reference manual. When you work with text, you should look at those string functions at Working with text is particularly important when checking the input of users into your form. I am looking at just a few of examples here. You really need to read the reference to see what is available.

trim() trim(string) removes the whitespace at the beginning and the end of the string string. It returns the transformed string. $input=" Festbock "; $output=trim($input); print "|$output|";// prints: |Festbock| whitespace is any of the following characters –the blank character –the newline –the carriage return –the tabulation character

strlen() strlen(string) returns the length of the string string. $zip=trim($_POST['zipcode']); $zip_length=strlen($zip); print $zip_length; // hopefully, prints 5

strip_tags() strip_tags(string) removes HTML tags from the string string $input=" But weiser"; print strip_tags($input); // prints: Butweiser $in=" "; print strip_tags($in); // prints nothing, hurray!

htmlspecialchars() htmlspecialchars(string) makes XML entities out of XML special characters in the string string.,&, and " are transformed. It returns the transformed string. $in="What does the element do?"; print htmlspecialchars($in); // prints: What does the <div> element do? Using htmlspecialchars() is considered to be good security because it prevents injection of HTML and especially its element.

substr() substr( string, start, offset) returns the substring of a string string starting at position start, with length offset. $string=I like beer.; $sub=substr($string, 2, 4); print $sub; // prints like

documentation: description string trim ( string $str [, string $charlist ] ) here "string" at the beginning tells us that the function returns a variable of type string. The name of the function is written in bold. Then comes the parenthesis. It encloses the arguments. Anything that is optional is enclosed in square brackets. Don't put the square bracket in the function call.

argument list string $str [, string $charlist ] This argument list suggests –that you have to give a first argument that is a string (or will be converted to it) –that there is an optional argument that also is a string –arguments are separated by comma. The arguments are described in the parameters section.

return value There is a section in the description for the returned value. You will be told what type. You will be told what it means.

more number functions abs() calculates the absolute value print abs(-3)// prints: 3 print abs(3)// prints: 3 max() and min() return maximum and minimum print min(2,3)// prints: 2 rand( min, max ) returns a random integer between the integers min and max, included. The list of functions that use numbers is

variable types Variables in PHP have types. You can check for types is_numeric() is_string() is_int() is_float() They all return a Boolean value. They can be used to check the nature of a variable.

arrays The variables we have looked at up until now are scalars. They only contain one piece of data. Arrays are variables that can contain more than one piece of data. –For example, a six pack in conveniently represented as an array of cans of beer. –For another example, a class is a group of people, each having a name, a social security number, etc.

numeric arrays An numeric array has key value pairs where the keys are numbers. $good_beers[0]="Baltika 8"; $good_beers[1]="Bruch Festbock"; or as follows $lousy_beers=array("Miller Lite", "Sam Adams", "Budweiser"); print $lousy_beers[0];// prints: Miller Lite print $lousy_beers[2];// prints: Budweiser

keeping count in numeric arrays For numeric arrays, you can add members very simple without keeping track of number. $beers=array("Karlsberg", "Bruch") ; $beers[]="Budweiser"; // $beer now has Karlberg, Bruch and Budweiser print count($beers) ; // prints 3

string arrays Sometimes you need data structured by a string. For example for a price list. $price['Grosswald Export']=1.45; $price['Bruch Festbock']=1.74; // the array $price has strings as keys An equivalent way to declare this is $price=array('Grosswald Export' => 1.45, 'Bruch Festbock' => 1.74);

array functions There is a very large number of array functions. They are described in the array function reference. Now we are just looking at some examples.

count() count() returns the size of an array $price['Grosswald Export']=1.45; $price['Bruch Festbock']=1.74; $product_number=count($price); print "We have $product_number products for you today."; // prints: We have 2 products for you today.

unset() This can be used to unset an element $beers_drunk('Amstel' => 'good', 'Miller' =>'ok', 'Budweiser'=>'lousy'); unset($beers_drunk('Amstel'); Now the array $beers_drunk only has two elements.

foreach() {} loop, numeric array The foreach loop loops over arrays. You use it as foreach($array as $element). The array $array is the array you are looping through. Each time you reach a new element, the current element is placed in $element.

a foreach() example $bottles=array('Amstel', 'Karlsberg', 'Sam Adams'); foreach($bottles as $beer) { print "Thomas has a $beer,\n"; } // prints: // "Thomas has a Amstel, // Thomas has a Karlsberg, // Thomas has a Sam Adams,"

foreach() loop, string array The foreach loop loops over arrays. You use it as foreach( $array as $key => $value ). The array $array is the array you are looping through. Each time you reach a new element, the current key is placed in $key and the value in $value.

another foreach() example Recall the $price string array. Another example illustrates print " \n"; foreach ($price as $item => $euro_amount) { print " $item \n"; print " €$euro_amount \n"; } print " "; This prints the full price list. But it could also do the whole form. This is fabulous!

foreach() example from the form $_GET is an array. You can loop through it. foreach($_GET as $control => $value) { print you set $control to $value \n; }

the well-aligned price table $l_r=array('left','right'); $count=0; // counter of elements printed print " \n"; foreach ($price as $item => $euro_amount) { print " <td align=\"$l_r[$count % 2]\""; print "$item"; $count++; print " \n €$euro_amount \n"; $count++; }

print " \n"; // This produces something like // // Grosswald Export // €1.45 // Bruch Festbock // €1.74 //

Thank you for your attention! Please switch off machines b4 leaving!