Arrays and Strings. Arrays in PHP Arrays are made up of multiple memory blocks, each with the same name and differentiated by an index number Each block.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Computer Science & Engineering 2111 Text Functions 1CSE 2111 Lecture-Text Functions.
Designing Algorithms Csci 107 Lecture 4. Outline Last time Computing 1+2+…+n Adding 2 n-digit numbers Today: More algorithms Sequential search Variations.
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
Designing Algorithms Csci 107 Lecture 4.
Chapter 8 Arrays and Strings
1 Lab Session-III CSIT-120 Spring 2001 Revising Previous session Data input and output While loop Exercise Limits and Bounds GOTO SLIDE 13 Lab session.
Chapter 8: String Manipulation
JSP Standard Tag Library
Lecture 7: Perl pattern handling features. Pattern Matching Recall =~ is the pattern matching operator A first simple match example print “An methionine.
Lists in Python.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 9 More About Strings.
Sys.Prog & Scripting - HW Univ1 Systems Programming & Scripting Lecture 18: Regular Expressions in PHP.
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.
Class 2Intro to Databases Goals of this class Include & Require in PHP Generating Random Numbers in PHP Arrays – Numerically Indexed and Associative Program.
1 CSC 221: Computer Programming I Fall 2004 Lists, data access, and searching  ArrayList class  ArrayList methods: add, get, size, remove  example:
1Computer Sciences Department Princess Nourah bint Abdulrahman University.
PHP Constructs Advance Database Management Systems Lab no.3.
Chapter 15 Introduction to PL/SQL. Chapter Objectives  Explain the benefits of using PL/SQL blocks versus several SQL statements  Identify the sections.
5 1 Data Files CGI/Perl Programming By Diane Zak.
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting Files & Directories.
An Introduction to Programming with C++ Sixth Edition Chapter 13 Strings.
8 1 String Manipulation CGI/Perl Programming By Diane Zak.
Unit – 3 Control structures. Condition Statements 1.If.…..else :- Has someone ever told you, "if you work hard, then you will succeed"? And what happens.
PHP Tutorial. What is PHP PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages.
CGS 3066: Web Programming and Design Spring 2016 PHP.
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Quiz 3 this week – last section on Friday. Assignment 4 is posted. Data mining: –Designing functions.
© A+ Computer Science - Magpie Magpie is a lab that focuses on classes, randomness, and Strings. This lab will make sure that you.
ARRAYS (Extra slides) Arrays are objects that help us organize large amounts of information.
Information and Computer Sciences University of Hawaii, Manoa
>> Fundamental Concepts in PHP
CS 330 Class 7 Comments on Exam Programming plan for today:
Pointers and Linked Lists
David Meredith Aalborg University
Computer Programming BCT 1113
Containers and Lists CIS 40 – Introduction to Programming in Python
Arrays An array in PHP is an ordered map
Miscellaneous Items Loop control, block labels, unless/until, backwards syntax for “if” statements, split, join, substring, length, logical operators,
Engineering Innovation Center
CSc 110, Spring 2018 Lecture 32: Sets and Dictionaries
MATLAB: Structures and File I/O
Prof. Neary Adapted from slides by Dr. Katherine Gibson
Lists Part 1 Taken from notes by Dr. Neil Moore & Dr. Debby Keen
Arrays, For loop While loop Do while loop
The relational operators
Topics Introduction to File Input and Output
© Akhilesh Bajaj, All rights reserved.
In Class Program: Today in History
Week 9 – Lesson 1 Arrays – Character Strings
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.
© A+ Computer Science - Arrays and Lists © A+ Computer Science -
CIS16 Application Development and Programming using Visual Basic.net
PHP.
Coding Concepts (Data- Types)
Text Analyzer BIS1523 – Lecture 14.
Representation and Manipulation
Control Structures Part 1
Functions continued.
Topics Basic String Operations String Slicing
Arrays Arrays A few types Structures of related data items
Introduction to Computer Science
Arrays.
Algorithmic complexity
Topics Basic String Operations String Slicing
Topics Introduction to File Input and Output
CIS 136 Building Mobile Apps
SEEM 4540 Tutorial 4 Basic PHP based on w3Schools
Topics Basic String Operations String Slicing
Lecture Set 9 Arrays, Collections, and Repetition
Introduction to Computer Science
Presentation transcript:

Arrays and Strings

Arrays in PHP Arrays are made up of multiple memory blocks, each with the same name and differentiated by an index number Each block is referred to as an element of the array The format for creating an array in PHP is: $data = array("foo", "bar", "hello", "world"); $data now contains an array with 4 elements $data[0] contains “foo”, $data[1] contains “bar” and so on

Adding/Removing Elements $array[]=value;  adds an element unset($array[5]);  removes the 6 th element unset($array);  deletes the whole array Note: there are other ways to add/remove elements i.e. array_splice can be used to remove elements and/or replace them with others

Parsing Arrays For loops are commonly used to iterate through the elements of an array The process of searching for an item in an array 1 element at a time is referred to a linear search, a very simple searching algorithm The foreach loop is a modified for loop that is made for this purpose: Example: foreach ($array as $i => $value) { if($value==“what we’re looking for”){ echo “We found it at position “.$i; } } $i is the index variable that stores the index of each element and $value is the actual value of the element We could also use a standard for loop: for ($i = 0; $i < count($array); $i++) { if($array[$i]==“what we’re looking for”){ echo “We found it at position “.$i; } } Note the use of the count() function which returns the number of elements in an array

PHP Arrays An array in PHP is actually an ordered map. A map is a type that associates values to keys.array $array = array( "foo" => "bar", "bar" => "foo", ); array[‘foo’]  returns ‘bar’

Strings Strings are the common format for storing data in programs As a result its very important to understand how to parse out critical information from a string We do this by using functions The following slides will cover common PHP String functions

explode explode — Split a string by string Description ¶ array explode ( string $delimiter, string $string [, int $limit = PHP_INT_MAX ] ) Returns an array of strings, each of which is a substring of string formed by splitting it on boundaries formed by the string delimiter. Parameters Delimiter The boundary string. String The input string. Limit If limit is set and positive, the returned array will contain a maximum of limit elements with the last element containing the rest of string. If the limit parameter is negative, all components except the last -limit are returned. If the limit parameter is zero, then this is treated as 1.

chop(chop(string,charlist ) ) Removes whitespace or other characters from the right end of a string (charlist is optional) str_ireplace()Replaces some characters in a string (case- insensitive) ord(string)Returns the ASCII value of the first character of a string str_split(string, length) Splits a string into an array (length is optional) chr(ascii)Returns a character from a specified ASCII value strlen(string)Returns the length of a string strpos(strpos(string,find,s tart)) Returns the position of the first occurrence of a string inside another string (case-sensitive)- start is optional

User-Defined Functions A function may be defined using syntax such as the following:

Example function sum($num1,$num2) { $answer=$num1+num2; return $answer; }

Exercises Modify your user registration script so that it now does the following: When a user registers you will read the current list of registered users from the file into a variable. Explode the string into an array using the end of line delimiter(\r\n). Iterate through each element of this array and store it in another variable. Explode each of these lines using the comma delimeter into its own array. Next determine if the user who just registered is already a registered user and if so do not add them to the list. Provide an appropriate message under either circumstance. Make sure you check the username by matching any possible variation i.e. Mike, mike, MIKE etc. Hint: use the strncasecmp() function (read the doc.’s to learn how to use this in this situation).strncasecmp()

Modification-Using a Function Modify your code from the previous example to include a function that takes a line from the file, a name and then returns true if the name is in the line and false if it isn’t.