Slide 1 PHP Arrays and User Defined Functions ITWA133.

Slides:



Advertisements
Similar presentations
Classes & Objects INTRODUCTION : This chapter introduces classes ; explains data hiding, abstraction & encapsulation and shows how a class implements these.
Advertisements

Copyright © 2002 Pearson Education, Inc. Slide 1.
Introduction to C Programming
Chapter 7: Arrays In this chapter, you will learn about
Intro to Scala Lists. Scala Lists are always immutable. This means that a list in Scala, once created, will remain the same.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 7: User-Defined Functions II.
Chapter 7: User-Defined Functions II
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
Chapter 7: User-Defined Functions II Instructor: Mohammad Mojaddam.
PHP Functions and Control Structures. 2 Defining Functions Functions are groups of statements that you can execute as a single unit Function definitions.
ITC 240: Web Application Programming
PHP Functions Composite Types. Functions  Declaration  function functionName(paramList) {  /* code goes here */ }  paramList is comma-separated list.
VBA Modules, Functions, Variables, and Constants
Case, Arrays, and Structures. Summary Slide  Case Structure –Select Case - Numeric Value Example 1 –Select Case - String Value Example  Arrays –Declaring.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 8 Arrays.
Chapter 8 Arrays and Strings
Arrays. Objectives Learn about arrays Explore how to declare and manipulate data into arrays Learn about “array index out of bounds” Become familiar with.
Introduction to Methods
1 CS101 Introduction to Computing Lecture 29 Functions & Variable Scope (Web Development Lecture 10)
2010/11 : [1]Building Web Applications using MySQL and PHP (W1)PHP Recap.
A Level Computing#BristolMet Session Objectives U2#S6 MUST identify different data types used in programming aka variable types SHOULD describe each data.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
CSEB114: PRINCIPLE OF PROGRAMMING Chapter 8: Arrays.
Mr. Justin “JET” Turner CSCI 3000 – Fall 2015 CRN Section A – TR 9:30-10:45 CRN – Section B – TR 5:30-6:45.
Chapter 8 Arrays and Strings
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
Chapter 6: User-Defined Functions
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
Project 1 Due Date: September 25 th Quiz 4 is due September 28 th Quiz 5 is due October2th 1.
CMPS 211 JavaScript Topic 2 Functions and Arrays.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Arrays.
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
Interpretation Environments and Evaluation. CS 354 Spring Translation Stages Lexical analysis (scanning) Parsing –Recognizing –Building parse tree.
Web Database Programming Week 3 PHP (2). Functions Group related statements together to serve “a” specific purpose –Avoid duplicated code –Easy maintenance.
PHP Programming with MySQL Slide 4-1 CHAPTER 4 Functions and Control Structures.
Slide 1 PHP Operators and Control Structures ITWA 133.
Chapter 2 Functions and Control Structures PHP Programming with MySQL 2 nd Edition.
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.
1Computer Sciences Department Princess Nourah bint Abdulrahman University.
CPS120: Introduction to Computer Science Functions.
CPS120: Introduction to Computer Science Lecture 14 Functions.
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
CHAPTER 7 Introduction to PHP5 Part II อ. ยืนยง กันทะเนตร คณะเทคโนโลยีสารสนเทศและการสื่อสาร มหาวิทยาลัยพะเยา 1.
Review for Final Exam. Contents 5 questions (20 points each) + 1 bonus question (20 points) – Basic concepts in Chapters 1-4 – Chapters 5-9 – Bonus: Chapter.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
Functions Structured Programming. Topics to be covered Introduction to Functions Defining a function Calling a function Arguments, local variables and.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Arrays Chapter 7. MIS Object Oriented Systems Arrays UTD, SOM 2 Objectives Nature and purpose of an array Using arrays in Java programs Methods.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
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.
11 – Introduction to PHP(1) Informatics Department Parahyangan Catholic University.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Arrays.
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.
Lesson 3 Functions. Lesson 3 Functions are declared with function. For example, to calculate the cube of a number function function name (parameters)
Arrays Chapter 7.
Programming Right from the Start with Visual Basic .NET 1/e
Chapter 7: User-Defined Functions II
Class06 Arrays MIS 3502 Jeremy Shafer Department of MIS
User-Defined Functions
Arrays MIS 3502 Jeremy Shafer Department of MIS Fox School of Business
Chapter 6 Methods: A Deeper Look
Review for Final Exam.
Object Oriented Programming in java
Chapter 7: User-Defined Functions II
PHP an introduction.
Presentation transcript:

Slide 1 PHP Arrays and User Defined Functions ITWA133

Murach’s ASP.NET 3.5/C#, C1© 2008, Mike Murach & Associates, Inc. Slide 2 PHP Arrays and User Defined Functions Objectives To know the different approach of using arrays in PHP. To be familiar with using print_r function and var_dump function for array inspection. To use PHP lazy function foreach to iterate through array elements. To implement one dimensional and multi-dimensional in the program. To know what is associative array in PHP. To use different predefined functions in manipulating array elements. To defined what is function and to use function keyword in declaring function in PHP. To create a user defined function using different approach.

Murach’s ASP.NET 3.5/C#, C1© 2008, Mike Murach & Associates, Inc. Slide 3 PHP Arrays and User Defined Functions Objectives (continue) To know use variables that is globally declared and locally declared. To create static variables that can be used in accumulating data every time the function calls.

Slide 4 PHP Arrays

Slide 5 PHP Arrays and User Defined Functions Array is used to aggregate a series of similar items together, arranging and dereferencing them in some specific way. Each member of the array index references a corresponding value and can be a simple numerical reference to the value’s position in the series, or it could have some direct correlation to the value. PHP array does not need to declare how many elements that the array variable have. Array index in PHP can be also called as array keys. Array can be used as ordinary array same as in C and C++ arrays.

Slide 6 PHP Arrays and User Defined Functions Array: one dimensional Example: Code 1 Output: Example: Code 2 Example: Code 3

Slide 7 PHP Arrays and User Defined Functions Array: Functions for visualizing arrays print_r() function short for print recursive. This takes an argument of any type and prints it out, which includes printing all its parts recursively. var_dump() function is same as the print_r function except that it prints additional information about the size and type of the values it discovers print_r() and var_dump() functions are commonly used for debugging. The point of this of these functions is to help you visualize what’s going on with compound data structures like arrays.

Slide 8 PHP Arrays and User Defined Functions Array: Functions for visualizing arrays Example: using print_r() Output: Example: using var_dump() Output:

Slide 9 PHP Arrays and User Defined Functions Array: Looping through array elements foreach() function is a statement used to iterate or loop through the element in an array. With each loop, a foreach statement moves to the next element in an array. foreach statement specify an array expression within a set of parenthesis following the foreach keyword. Syntax

Slide 10 PHP Arrays and User Defined Functions Array: Looping through array elements Example: Output: $arr The name of the array that you’re walking through. $key The name of the variable where you want to store the key. (optional) $value The name of the variable where you want to store the value

Slide 11 PHP Arrays and User Defined Functions Array: Looping through array elements Example: Output:

Slide 12 PHP Arrays and User Defined Functions Arrays: Multidimensional arrays Example: Output:

Slide 13 PHP Arrays and User Defined Functions Arrays: Sorting

Slide 14 PHP Arrays and User Defined Functions Arrays: Sorting Example: Output:

Slide 15 PHP Arrays and User Defined Functions Arrays: Sorting Example: Output:

Slide 16 PHP Arrays and User Defined Functions Arrays: Sorting Example:Output:

Slide 17 PHP User Defined Functions

Slide 18 PHP Arrays and User Defined Functions Functions is a group of PHP statements that performs a specific task. Functions are designed to allow you to reuse the same code in different locations. User defined functions functions that are provided by the user of the program. Predefined functions functions that are built-in into PHP to perform some standard operations

Slide 19 PHP Arrays and User Defined Functions Functions: User defined functions Syntax function name(param){ //code to be executed by the function } Where function – is the keyword used to declare a function name – is the name of the function or function identifier param – is the formal parameters of the function. Parameter must follow the rule of naming dentifier.

Slide 20 PHP Arrays and User Defined Functions Functions: Function with no parameters Example:Output:

Slide 21 PHP Arrays and User Defined Functions Functions: Function with parameters Example:Output:

Slide 22 PHP Arrays and User Defined Functions Functions: Function that returns a value Example:Output:

Slide 23 PHP Arrays and User Defined Functions Functions: Nested function Example: Output: Example:Output:

Slide 24 PHP Arrays and User Defined Functions Functions: Variable scope Global Variables is one that declared outside a function and is available to all parts of the program. Local Variables is declared inside a function and is only available within the function in which it is declared. Static Variables is used to retain the values calls to the same function.

Slide 25 PHP Arrays and User Defined Functions Functions: using variables Example:Output: Example:Output:

Slide 26 PHP Arrays and User Defined Functions Functions: using variables Example:Output: Example:Output:

Slide 27 PHP Arrays and User Defined Functions Functions: using variables Example: Output:

Slide 28 An introduction to PHP web programming Summary Array is used to aggregate a series of similar items together. Array index references a corresponding value. Array index can be simple numerical or have some direct correlation to the value. Array index is also known as Array Keys. print_r function is used to print the array structure. var_dump function is same as print_r function except it adds additional information about the data of each element. The foreach statement is use to iterate through the element in an array. Using foreach statement you can display both the keys and value of each element in the array. PHP provides functions for array manipulation such as sort(), rsort(), asort(), arsort(), ksort(), krsort(), and usort() functions.

Slide 29 An introduction to PHP web programming Summary (continue) sort(), asort(), and ksort() functions are used to sort elements in the array in ascending order. rsort(), arsort(), and krsort() functions are used to sort elements in the array in descending order. sort() and rsort() does not maintain its index reference for each values. asort(), ksort(), arsort(), and krsort() maintains its reference for each values. asort() and arsort() used to sort elements by values. ksort() and krsort() used to sort elements by keys. Functions is a group of PHP statements that performs a specific task. Functions can be user defined generally defined by the user of the program and predefined that are build in using libraries.

Slide 30 An introduction to PHP web programming Summary (continue) You use functions in different ways. Function can only do something without passing values. You can pass values to a function and you can ask functions to return a value. function keyword is used in PHP to declare a function. A function that is declared inside a function is said to be hidden. To gain access to a variable that is outside from the function we use the global keyword. We use static keyword to declare a variable inside a function that will act as accumulator variable this will let the program remember the last value of the variable that was used.