PHP Functions Composite Types. Functions  Declaration  function functionName(paramList) {  /* code goes here */ }  paramList is comma-separated list.

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

1 Storage Duration and Scope –Local and global variables Storage classes –automatic, static, external, register Todays Material.
Objectives Using functions to organize PHP code
PHP Functions and Control Structures. 2 Defining Functions Functions are groups of statements that you can execute as a single unit Function definitions.
CIT 590 Intro to Programming Java lecture 4. Agenda Types Collections – Arrays, ArrayLists, HashMaps Variable scoping Access modifiers – public, private,
Javascript Client-side scripting. Up to now  We've seen a little about how to control  content with HTML  presentation with CSS  Javascript is a language.
Chapter 4 Functions and Control Structures PHP Programming with MySQL.
PHP Server-side Programming. PHP  PHP stands for PHP: Hypertext Preprocessor  Originally Personal Home Page  PHP is interpreted  PHP code is embedded.
Subroutines. aka: user-defined functions, methods, procdures, sub-procedures, etc etc etc We’ll just say Subroutines. –“Functions” generally means built-in.
CS 106 Introduction to Computer Science I 03 / 17 / 2008 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.
Programming Concepts MIT - AITI. Variables l A variable is a name associated with a piece of data l Variables allow you to store and manipulate data in.
Chapter 6: Function. Scope of Variable A scope is a region of the program and broadly speaking there are three places, where variables can be declared:
Multiple Choice Solutions True/False a c b e d   T F.
PHP MOHAMMED SHURRAB TO MISS/ RASHA ATTALLAH. What is PHP? Stands for "PHP Hypertext Preprocessor" Server-side scripting language HTML-embedded Supports.
INTERNET APPLICATION DEVELOPMENT For More visit:
Class 3Intro to Databases Arrays Sending Values to a Script Manually For and While Loops.
Introduction to Python Basics of the Language. Install Python Find the most recent distribution for your computer at:
Tutorial 2 Variables and Objects. Working with Variables and Objects Variables (or identifiers) –Values stored in computer memory locations –Value can.
Javascript. Outline Introduction Fundamental of JavaScript Javascript events management DOM and Dynamic HTML (DHTML)
Mr. Justin “JET” Turner CSCI 3000 – Fall 2015 CRN Section A – TR 9:30-10:45 CRN – Section B – TR 5:30-6:45.
JavaScript, Fourth Edition
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
Week 7. Lecture 2 Functions, Arrays, PHP&MySQL. Function with More than one argument and a return statement For a function to return a value, the return.
Multidimensional Arrays CIT 336. The Basics Explaining Multidimensional Arrays.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Arrays.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Java Script: Arrays (Chapter 11 in [2]). 2 Outline Introduction Introduction Arrays Arrays Declaring and Allocating Arrays Declaring and Allocating Arrays.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
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.
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.
Array & Foreach อาร์เรย์และคำสั่งวนลูป. Content 1. Definition and Usage 2. Syntax 3. print_r() Statement 4. For and Foreach 5. Array Functions.
Class 2Intro to Databases Goals of this class Include & Require in PHP Generating Random Numbers in PHP Arrays – Numerically Indexed and Associative Program.
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.
ECE122 Feb. 22, Any question on Vehicle sample code?
Sys Prog & Scripting - HW Univ1 Systems Programming & Scripting Lecture 16: PHP Types.
Slide 1 PHP Arrays and User Defined Functions ITWA133.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Scope When we create variables and functions, they are limited in where they are visible and where they can be referenced For the most part, the identifiers.
Topic 4:Subroutines CSE2395/CSE3395 Perl Programming Learning Perl 3rd edition chapter 4, pages 56-72, Programming Perl 3rd edition pages 80-83,
OOP in C++ CS 124. Program Structure C++ Program: collection of files Source (.cpp) files be compiled separately to be linked into an executable Files.
More Perl Data Types Scalar: it may be a number, a character string, or a reference to another data type. -the sigil $ is used to denote a scalar(or reference)
CHAPTER 7 Introduction to PHP5 Part II อ. ยืนยง กันทะเนตร คณะเทคโนโลยีสารสนเทศและการสื่อสาร มหาวิทยาลัยพะเยา 1.
PROGRAMMING IN C#. Collection Classes (C# Programming Guide) The.NET Framework provides specialized classes for data storage and retrieval. These classes.
Rich Internet Applications 2. Core JavaScript. The importance of JavaScript Many choices open to the developer for server-side Can choose server technology.
COMPUTER PROGRAMMING 2 ArrayLists. Objective/Essential Standard Essential Standard 3.00Apply Advanced Properties of Arrays Essential Indicator 3.02 Apply.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
CS 106 Introduction to Computer Science I 03 / 22 / 2010 Instructor: Michael Eckmann.
PHP Reusing Code and Writing Functions 1. Function = a self-contained module of code that: Declares a calling interface – prototype! Performs some task.
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,
JavaScript, Sixth Edition
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.
PHP Tutorial. What is PHP PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages.
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.
ITM 3521 ITM 352 Functions. ITM 3522 Functions  A function is a named block of code (i.e. within {}'s) that performs a specific set of statements  It.
Coming up Implementation vs. Interface The Truth about variables Comparing strings HashMaps.
Lesson 3 Functions. Lesson 3 Functions are declared with function. For example, to calculate the cube of a number function function name (parameters)
Arrays in PHP are quite versatile
CHAPTER 5 SERVER SIDE SCRIPTING
CIIT-Human Computer Interaction-CSC456-Fall-2015-Mr
STL - Algorithms.
Object Oriented Programming in java
PHP an introduction.
Web Programming and Design
Presentation transcript:

PHP Functions Composite Types

Functions  Declaration  function functionName(paramList) {  /* code goes here */ }  paramList is comma-separated list of param names with optional default values  use return to return a value  Calling a function  functionName( argList)  argList is comma-separated list of expressions - number must match declaration

Variable scope  Variables defined outside of any function are global  Variables defined inside a function are local to the function (as are parameters)  Using global variables in a function  $GLOBALS is array containing all global variables  global keyword

Compound Types in PHP  PHP has two compound types  Arrays -arrays in PHP are really ordered maps  multi-dimensional arrays are arrays whose elements are arrays  Objects - variables whose type is defined by a class

Arrays  An array consists of a collection of elements  each element has a key and a value (like a hash table)  arrays with only numeric keys are a special case  you can use arrays with numeric keys the same way you do an array in C or Java

Creating an array  Create dynamically by assigning values to elements  $veggie['corn'] = 'yellow';  $dinner[0] = 'Lemon Chicken';  Use the Array constructor  $veggie = array( 'corn' => 'yellow', 'beet' => 'red', 'carrot' => 'orange');

Creating a numeric array  Use array with just a list of values  keys will automatically be numbers (starting from 0) $dinner = ('Sweet Corn and Asparagus', 'Lemon Chicken', 'Spicy Eggplant');  Add new elements to end of list by assigning with no index  $dinner[] = 'Braised Bamboo fungus';

Using arrays  count($arrayName) gives the number of elements in the array  implode(delim, array) creates a string from an array  $menu = implode( ', ', $dinner);  explode(delim, string) creates a numeric array from a string  $dinner = explode( ', ', $menu);

Sorting arrays  The array elements will be rearranged by these methods  sort() / rsort() sort by element values (ascending/descending)  asort() / arsort() sort by element value, keeping keys and values together  ksort() / krsort() sort by key, keeping keys and values together

Looping with arrays  Use foreach to loop through elements of array foreach( $meal as $key => $value) print "$key $value\n"; foreach( $dinner as $dish) print "You can eat $dish.\n"  foreach goes through elements in order they were added to array  not necessarily numeric order

Classes and Objects  PHP supports object-oriented programming  Class is a template describing both data and operations  Method is a function defined in a class  Property is a variable defined in a class  Constructor is used to create instances (objects)  Static method doesn't need an object

Sample Class definition class Cart { var $items; // Items in our shopping cart // Add $num articles of $artnr to the cart function add_item($artnr, $num) { $this->items[$artnr] += $num;} // Take $num articles of $artnr out of the cart function remove_item($artnr, $num) { if ($this->items[$artnr] > $num) { $this->items[$artnr] -= $num; return true; } elseif ($this->items[$artnr] == $num) { unset($this->items[$artnr]); return true; } else { return false; } }

Constructors  Constructor is a method with the same name as the class  Use it to initialize properties class Item { var $catalogNum, $price; function Item($id='none', $cost='0.0') { $catalogNum=$id; $price = $cost; } … }

Using Objects  Use new to create objects  $myItem = new Item( 'AB234', 3.95);  Access methods and properties with -> operator  $charge = $myItem->price;

Sources  Learning PHP 5 by David Sklar  Programming PHP by Rasmus Ledorf and Kevin Tatroe  PHP home page 