Arrays. Indexed Arrays Use numbers as keys: $grades = array(); $grades[0] = ' A ' ; $grades[1] = ' A- ' ; $grades[2] = ' B+ ' ; Or with one statement:

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

Session 1 & 2BBK P1 Module5-May-2007 : [‹#›] PHP: Moving On..
Programming Logic and Design Sixth Edition
Objectives Using functions to organize PHP code
1 PHP Statement Constructs Server Scripting. 5-2 Basic Statement All Statements end in a semicolon. Statements are delimited from the HTML code by enclosing.
PHP Using Arrays.
PHP Functions and Control Structures. 2 Defining Functions Functions are groups of statements that you can execute as a single unit Function definitions.
Outline IS400: Development of Business Applications on the Internet Fall 2004 Instructor: Dr. Boris Jukic PHP.
Working with JavaScript. 2 Objectives Introducing JavaScript Inserting JavaScript into a Web Page File Writing Output to the Web Page Working with Variables.
Hashes a “hash” is another fundamental data structure, like scalars and arrays. Hashes are sometimes called “associative arrays”. Basically, a hash associates.
Arrays  Writing a program that uses a large amount of information.  Such as a list of 100 elements.  It is not practical to declare.
Chapter 4 Functions and Control Structures PHP Programming with MySQL.
XP 1 Working with JavaScript Creating a Programmable Web Page for North Pole Novelties Tutorial 10.
PHP Server-side Programming. PHP  PHP stands for PHP: Hypertext Preprocessor  PHP is interpreted  PHP code is embedded into HTML code  interpreter.
1 CS428 Web Engineering Lecture 19 Data Types (PHP - II)
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.
PHP Tutorials 02 Olarik Surinta Management Information System Faculty of Informatics.
Intro to Web Programming using PHP, HTTP, CSS, and Javascript Layton Smith CSE 4000.
Introduction to PHP and Server Side Technology. Slide 2 PHP History Created in 1995 PHP 5.0 is the current version It’s been around since 2004.
Using Arrays in PHP (Part I) Lecture # 6. What is an array? It is a collection of multiple values assembled into one variable An array can consist of.
INTERNET APPLICATION DEVELOPMENT For More visit:
Class 3Intro to Databases Arrays Sending Values to a Script Manually For and While Loops.
The foreach LooptMyn1 The foreach Loop The foreach loop gives an easy way to iterate over arrays. foreach works only on arrays, and will issue an error.
2010/11 : [1]Building Web Applications using MySQL and PHP (W1)PHP Recap.
PHP H ypertext P re-processor. Unit 6 - PHP - Hello World! - Data types - Control structures - Operators.
Arrays IDIA Spring 2012 Bridget M. Blodgett.
NMED 3850 A Advanced Online Design January 26, 2010 V. Mahadevan.
Chap 3 – PHP Quick Start COMP RL Professor Mattos.
XP Tutorial 10New Perspectives on Creating Web Pages with HTML, XHTML, and XML 1 Working with JavaScript Creating a Programmable Web Page for North Pole.
Chapter 8: Arrays.
Open Source Software Unit – 3 Presented By Mr. R.Aravindhan.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 5 Arrays.
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.
PHP Conditional Statements Conditional statements in PHP are used to perform different actions based on different conditions. Conditional Statements Very.
PHP Programming with MySQL Slide 4-1 CHAPTER 4 Functions and Control Structures.
Array & Foreach อาร์เรย์และคำสั่งวนลูป. Content 1. Definition and Usage 2. Syntax 3. print_r() Statement 4. For and Foreach 5. Array Functions.
1 CS428 Web Engineering Lecture 20 Control Structures, Loops and Pointers File Uploading Function (PHP - III)
PHP PHP: Hypertext Preprocesor Personal Home Page Tools.
Intro to PHP Carl-Erik Svensson. What is PHP? PHP is a widely-used general-purpose scripting language that is especially suited for Web development and.
CS 174: Web Programming September 2 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
Just a Little PHP Programming PHP on the Server. Common Programming Language Features Comments Data Types Variable Declarations Expressions Flow of Control.
Syntax : If ( Expression ) If ( Expression ) {Statements; } Example : ?>
PHP Open source language for server-side scripting Works well with many databases (e.g., MySQL) Files end in.php,.php3 or.phtml Runs on all major platforms.
Week 4 PHP H ypertext P reprocessor Reference : Official Site :
Slide 1 PHP Arrays and User Defined Functions ITWA133.
Data TypestMyn1 Data Types The type of a variable is not set by the programmer; rather, it is decided at runtime by PHP depending on the context in which.
XP Tutorial 10New Perspectives on HTML and XHTML, Comprehensive 1 Working with JavaScript Creating a Programmable Web Page for North Pole Novelties Tutorial.
Introducing Python CS 4320, SPRING Resources We will be following the Python tutorialPython tutorial These notes will cover the following sections.
David Lawrence 7/8/091Intro. to PHP -- David Lawrence.
הרצאה 4. עיבוד של דף אינטרנט דינמי מתוך Murach’s PHP and MySQL by Joel Murach and Ray Harris.  דף אינטרנט דינמי משתנה עפ " י הרצת קוד על השרת, יכול להשתנות.
Martin Kruliš by Martin Kruliš (v1.1)1.
Creating Databases for Web applications Server side vs client side PHP basics Homework: Get your own versions of sending working: both html and Flash!
 In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.  PHP Loops :  In.
Just a Little PHP Programming PHP on the Server. Common Programming Language Features Comments Data Types Variable Declarations Expressions Flow of Control.
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting PHP Basics.
Creating PHP Pages Chapter 5 PHP Structure and Syntax.
PHP Syntax You cannot view the PHP source code by selecting "View source" in the browser - you will only see the output from the PHP file, which is plain.
Visual C# 2005 Using Arrays. Visual C# Objectives Declare an array and assign values to array elements Initialize an array Use subscripts to access.
Dr. Abdullah Almutairi Spring PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages. PHP is a widely-used,
PHP Overview. What is PHP Widely available scripting language Free Alternative to Microsoft’s ASP Runs on the Web Server; not in the browser Example:
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.
XP Tutorial 10New Perspectives on HTML, XHTML, and DHTML, Comprehensive 1 Working with JavaScript Creating a Programmable Web Page for North Pole Novelties.
PHP using MySQL Database for Web Development (part II)
CIIT-Human Computer Interaction-CSC456-Fall-2015-Mr
Web Systems Development (CSC-215)
HYPERTEXT PREPROCESSOR BY : UMA KAKKAR
Web Programming Language
PHP an introduction.
Presentation transcript:

Arrays

Indexed Arrays Use numbers as keys: $grades = array(); $grades[0] = ' A ' ; $grades[1] = ' A- ' ; $grades[2] = ' B+ ' ; Or with one statement: $grades = array( ' A ', ' A- ', ' B+ ') ; Each item in the array is a key/value pair.

Associative Arrays Use strings as keys: $states = array(); $states[ ' NC ' ] = ' North Carolina ' ; $states[ ' CO ' ] = ' Colorado ' ; $states[ ' IA ' ] = ' Iowa ' ; Or: $states = array( ' NC ' => ' North Carolina ', ' CO ' => ' Colorado ', ' IA ' => ' Iowa ') ;

Output with Arrays Numeric keys don't need quotes: echo $grades[0]; Character-based keys do need quotes: echo $states['CO']; But output as part of a string is problematic. Options are: Concatenation: echo 'I grew up in '. $states['CO']; Curly brackets: echo "I grew up in {$states['CO']}"; Use a simpler variable: $myState=$states['CO']; Nesting quotes with associative arrays gives a parse error: echo "I grew up in $states['CO']"; //doesn't work!!

Alternative Array Creation $months = $array (1 => 'January', 'February', 'March', …); This sets the first numeric key value to 1. The remaining keys will be automatically generated incrementing by 1. The range() function can be used to create an array of sequential numbers: $die = range (1, 6); $lc_letters = range ('a', 'z');

Array Processing To access every array element use a foreach loop: $vowels = array('a', 'e', 'i', 'o', 'u'); foreach ($array as $value) { echo $value.' '; } //Don't forget to close the curly brace to end the loop //Output:

Array Processing Associative array example: $countryCodes = array('DEU' => 'Germany', 'JPN' => 'Japan', 'ARG' => 'Argentina'); foreach ($countryCodes as $key => $value) { echo "The code for $value is $key. "; } //Output:

Array Testing and Debugging The print_r() function will show you the contents of an array, both keys and values. print_r() is not intended for Web output. It is a testing and debugging tool. As such, it should be removed before the final version is published.

PHP's Built-in Superglobal Arrays Begin with $_ followed by an all caps name: Examples: $_GET $_POST $_SERVER $_FILES ◦These arrays are automatically populated by the PHP interpreter and are used frequently. ◦The syntax is exactly the same as for user-defined arrays.