Functions & Arrays. Functions Functions offer the ability for programmers to group together program code that performs specific task or function into.

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 Copyright © 2002 Pearson Education, Inc.. 2 Chapter 5 List Variables and Loops.
Copyright © 2003 Pearson Education, Inc. Slide 5-1 The Web Wizards Guide to PHP by David Lash.
Chapter 7 Introduction to Procedures. So far, all programs written in such way that all subtasks are integrated in one single large program. There is.
String and Lists Dr. Benito Mendoza. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list List.
ITC 240: Web Application Programming
1 Controlling Script Flow! David Lash Chapter 3 Conditional Statements.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 8 Multidimensional.
Concepts of Database Management Sixth Edition
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.
CSC 1701B Computing: Science and Creativity. Outline  Types  Variables  Operators  Control: sequence, selection, repetition  Functions (block headings.
Computer Science 1000 Spreadsheets II Permission to redistribute these slides is strictly prohibited without permission.
PHP Tutorials 02 Olarik Surinta Management Information System Faculty of Informatics.
Class 3Intro to Databases Arrays Sending Values to a Script Manually For and While Loops.
Copyright © 2003 Pearson Education, Inc. Slide 5-1 The Web Wizard’s Guide to PHP by David Lash.
NMED 3850 A Advanced Online Design January 26, 2010 V. Mahadevan.
REVIEW 2 Exam History of Computers 1. CPU stands for _______________________. a. Counter productive units b. Central processing unit c. Copper.
PHP flow of control & functions
Java Script: Arrays (Chapter 11 in [2]). 2 Outline Introduction Introduction Arrays Arrays Declaring and Allocating Arrays Declaring and Allocating Arrays.
Concepts of Database Management Seventh Edition
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.
Slide 3-1 CHAPTER 3 Conditional Statements Objectives To learn to use conditional test statements to compare numerical and string data values To learn.
Class 2Intro to Databases Goals of this class Include & Require in PHP Generating Random Numbers in PHP Arrays – Numerically Indexed and Associative Program.
Copyright © 2010 Certification Partners, LLC -- All Rights Reserved Perl Specialist.
Variables and ConstantstMyn1 Variables and Constants PHP stands for: ”PHP: Hypertext Preprocessor”, and it is a server-side programming language. Special.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 17 – Flag Quiz Application Introducing One-Dimensional.
Copyright © 2003 Pearson Education, Inc. Slide 5-1 The Web Wizard’s Guide to PHP by David Lash.
Chapter 2 Functions and Control Structures PHP Programming with MySQL 2 nd Edition.
1 Module 3 Conditional Statements. Objectives  Conditional test statements to compare numerical and string data values  Looping statements to repeat.
Just a Little PHP Programming PHP on the Server. Common Programming Language Features Comments Data Types Variable Declarations Expressions Flow of Control.
1Computer Sciences Department Princess Nourah bint Abdulrahman University.
© Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 8 Multidimensional Arrays.
PHP Constructs Advance Database Management Systems Lab no.3.
Slide 1 PHP Arrays and User Defined Functions ITWA133.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 8 Lists and Tuples.
Server-Side Scripting with PHP ISYS 475. PHP Manual Website
Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals.
Concepts of Database Management Seventh Edition Chapter 3 The Relational Model 2: SQL.
Slide 5-1 CHAPTER 5 Using Arrays for List Data Objectives To understand the benefits of using arrays in PHP To learn how to create and use sequential.
Chapter 8 Manipulating MySQL Databases with PHP PHP Programming with MySQL 2 nd Edition.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 6 Arrays.
PHP-5- Working with Files and Directories. Reading Files PHP’s file manipulation API is extremely flexible: it lets you read files into a string or into.
CST336, Dr. Krzysztof Pietroszek Week 2: PHP. 1.Introduction to PHP 2.Embed PHP code into an HTML web page 3.Generate (output HTML) web page using PHP.
1 An Introduction to R © 2009 Dan Nettleton. 2 Preliminaries Throughout these slides, red text indicates text that is typed at the R prompt or text that.
COMPUTER PROGRAMMING 2 ArrayLists. Objective/Essential Standard Essential Standard 3.00Apply Advanced Properties of Arrays Essential Indicator 3.02 Apply.
LISTS and TUPLES. Topics Sequences Introduction to Lists List Slicing Finding Items in Lists with the in Operator List Methods and Useful Built-in Functions.
 An array stores multiple values in one single variable.  Example: Output: I like Honda Civic, BMW and Toyota.
Copyright © 2003 Pearson Education, Inc. Slide 5-1 The Web Wizard’s Guide to PHP by David Lash.
Copyright © 2003 Pearson Education, Inc. Slide 4-1 The Web Wizard’s Guide to PHP by David Lash.
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,
Copyright © 2003 Pearson Education, Inc. Slide 3-1 The Web Wizard’s Guide to PHP by David A. Lash.
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.
11 – Introduction to PHP(1) Informatics Department Parahyangan Catholic University.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Arrays.
PHP Tutorial. What is PHP PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages.
String and Lists Dr. José M. Reyes Álamo. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list.
Concepts of Database Management, Fifth Edition Chapter 3: The Relational Model 2: SQL.
Introduction to Programming the WWW I CMSC Winter 2004 Lecture 8.
Lesson 3 Functions. Lesson 3 Functions are declared with function. For example, to calculate the cube of a number function function name (parameters)
Introduction to Programming
CHAPTER 5 SERVER SIDE SCRIPTING
CIIT-Human Computer Interaction-CSC456-Fall-2015-Mr
Introduction to Programming
The Web Wizard’s Guide to PHP by David Lash
The Web Wizard’s Guide to PHP by David Lash
Writing Functions( ) (Part 4)
PHP an introduction.
SEEM 4540 Tutorial 4 Basic PHP based on w3Schools
Presentation transcript:

Functions & Arrays

Functions Functions offer the ability for programmers to group together program code that performs specific task or function into a single unit hat can be used repeatedly throughout a program. A function is defined by name and is invoked by using its name. Functions can accept data in the form of arguments and can return results. PHP has several built-in functions. However, the flexibility of PHP lies in the ability of programmers to create their own functions.

Basic PHP Functions abs() Function: Returns the absolute value of a number. sqrt( ) Function: Take the square root of a single numerical argument. round( ) Function: Returns the number rounded up or down to the nearest integer. is_number( ) Function: Testing whether a variable is a valid number or numeric string. It returns true or false.

Basic PHP Functions rand( ) Function. Generate a random number. date( ) Function. Determine the current date and time.

PHP Function Site

Functions Functions are defined using the function statement. function function_name(parameters, arguments) { command block } function printName($name) { echo (“ Your Name is ”); echo $name; echo (“ ”); } printName(“Bob”);

Functions Returning Results: function cube($number) { $result = $number * $number * $number; return $result; } $y = cube(3);

Functions with Optional Arguments function OutputLine($text, $size=3, $color = “black”) { echo “ $text } OutputLine(“Good Morning”); // use size 3 and color black OutputLine(“Good Morning”, 4); //use size 4 and color black OutputLine(“Good Morning”, 4, “red”); //use size 4 and color red

Using External Script Files PHP supports two different functions for including external script files in your scripts: –require( ) : produces a fatal error if the external script can’t be inserted. –include( ) : produces a warning if can’t insert the specified file.

Example: External File //header.php <? $time = date(‘H:I’); function Calc_perc($buy, $sell) { $per = (($sell - $buy) / $buy) * 100; return $per; } ?> Example <? include(“header.php”); $buy = 2.50; $sell = echo “ It is $time.”; echo “We have hammers on special for \$$sell!”; $markup = Calc_perc($buy, $sell); echo “ Our markup is only $markup%!!”; ?>

Arrays An array is a PHP variable that can hold multiple data values (like a list of numbers, names, or grocery items) Sequential Array: Keeps track of these data items by using sequential numbers. Associative Array: Keeps track of these data items by using character strings.

Advantages of Arrays Include a flexible number of list items. Can add and delete items on the fly. Examine each item more concisely. You can use looping constructs in combination with arrays to look at and operate on each array item in a very concise manner. Use special array operators and functions. Built-in array operators and functions to do things such as count the number of items, sum the items, and sort the array.

Examples of Sequential Arrays $students = array(‘Johnson’, ‘Jackson’,’Jefferson’); $grades = array(66, 72, 89); echo “The first student is $student[0]”; echo “The second student is $student[1]”; $average = ($grades[0] + grades[1] + grades[2])/3; echo “The average of the grades is $average”; $sum = 0; for (i=0; i < count(grades); i++) $sum += $grades[i]; average = $sum/count(grades);

Use of “foreach” foreach ($student as $item) { echo (“$item”); } Output: Johnson Jackson Jefferson

Array Functions array_shift( ) :Remove an item from the beginning of an array. array_unshift( ) : Add an item to the beginning of an array. array_pop( ) : Remove an item from the end of an array. array_push( ) : Add an item to the end of an array.

Array Functions max( ) and min( ) functions : Determine largest and smallest numerical value in an array, respectively. array_sum( ) : Sum numerical values in the array. sort( ) : Reorder the items in numerical or alphabetical order.

Associative Arrays A string value index is used to look up or provide a cross-reference to the data value. Example: –$instructors = array(“Science” => “Smith”, “Math” => “Jones”, “English” => “Jacks”); echo “Instructor of Science is $instructors(‘Science’).” Output: Instructor of Science is Smith.

Associative Arrays foreach ($instructors as $subject => $teacher) { echo “Subject is $subject, teacher is $teacher ”; } Output: Subject is Science, teacher is Smith Subject is Math, teacher is Jones Subject is English, teacher is Jacks

Associative Arrays Adding an Associative Array Item: $instructors[“Language”] = “Pearson”; Deleting an Associative Array Item: unset($instructors[“Science”]); Verifying an Item’s Existence: if (isset($instructors[“Science”])) { echo (“Science is in the list.”); } else { echo (“Science is NOT in the list.”); }

Associative Arrays asort( ) : Sort an associative array by the values of the array while maintaining the relationship between indices and values. ksort( ) : Sort an associative array by the indices of the array while maintaining the relationship between indices and values.

Multidimensional Arrays Two-dimensional is a table: Example: Part No. Part Name Count Price AC10 Hammer AC11 Wrench $inventory = array ( ‘AC10’=>array(‘part’=>’Hammer’,’Count’=>122, ‘Price’=>12.50), ‘AC11’=>array(‘part’=>’Wrench’,’Count’=>5, ‘Price’=>5.50)); echo $inventory[‘AC10’][‘part’]; Output: Hammer