Faculty of Sciences and Social Sciences HOPE Functions in PHP Stewart Blakeway FML 213 0151 291 3113.

Slides:



Advertisements
Similar presentations
UFCE8V-20-3 Information Systems Development 3 (SHAPE HK) Lecture 3 PHP (2) : Functions, User Defined Functions & Environment Variables.
Advertisements

Core Java Lecture 4-5. What We Will Cover Today What Are Methods Scope and Life Time of Variables Command Line Arguments Use of static keyword in Java.
Outline What Is Function ? Create Function Call Function Parameters Functions Function Returning Values PHP Variable Scopes Passing by Reference Vs Value.
Faculty of Sciences and Social Sciences HOPE PHP – Working with Files and Strings Stewart Blakeway FML 213
Faculty of Sciences and Social Sciences HOPE Structured Problem Solving Week 8: Java: Selection and Repetition Stewart Blakeway.
Faculty of Sciences and Social Sciences HOPE Structured Problem Solving Week 7: Java basics Stewart Blakeway
Faculty of Sciences and Social Sciences HOPE PHP Flow Control Website Development Stewart Blakeway FML
Faculty of Sciences and Social Sciences HOPE Structured Problem Solving Week 12: Data Structures 1 Stewart Blakeway FML 213
Faculty of Sciences and Social Sciences HOPE PHP & MySQL Stewart Blakeway FML 213
Faculty of Sciences and Social Sciences HOPE PHP – Working with Input Stewart Blakeway FML 213
Faculty of Sciences and Social Sciences HOPE Structured Problem Solving Data Structures 3 Stewart Blakeway FML
Faculty of Sciences and Social Sciences HOPE Java: Loops within loops Stewart Blakeway FML 213
ITC 240: Web Application Programming
Advanced Package Concepts. 2 home back first prev next last What Will I Learn? Write packages that use the overloading feature Write packages that use.
Computer Science 103 Chapter 3 Introduction to JavaScript.
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
CSC 160 Computer Programming for Non-Majors Lecture #7: Variables Revisited Prof. Adam M. Wittenstein
CS61A Lecture 2 Functions and Applicative Model of Computation Tom Magrino and Jon Kotker UC Berkeley EECS June 19, 2012.
PHP – Get & Post; Functions; and Arrays IS6116 – 07 th February 2011.
Intermediate PHP (2) File Input/Output & User Defined Functions.
2010/11 : [1]Building Web Applications using MySQL and PHP (W1)PHP Recap.
Functions & Objects IDIA 618 Spring 2012 Bridget M. Blodgett.
Faculty of Sciences and Social Sciences HOPE JavaScript Validation Regular Expression Stewart Blakeway FML
PHP TUTORIAL. HISTORY OF PHP  PHP as it's known today is actually the successor to a product named PHP/FI.  Created in 1994 by Rasmus Lerdorf, the very.
Nael Alian Introduction to PHP
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single.
1 Functions 1 Parameter, 1 Return-Value 1. The problem 2. Recall the layout 3. Create the definition 4. "Flow" of data 5. Testing 6. Projects 1 and 2.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 13: An Introduction to C++
Faculty of Sciences and Social Sciences HOPE JavaScript Advanced Stewart Blakeway FML
Lecture 8 – Cookies & Sessions SFDV3011 – Advanced Web Development 1.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 15: More-Advanced Concepts.
1 Chapter 4 – Breaking It Up: Functions spring into PHP 5 by Steven Holzner Slides were developed by Jack Davis College of Information Science and Technology.
Chapter 2 User_defined Function. Chapter Goals In this chapter, you’ll learn all about PHP functions, including how : to create and invoke them, pass.
13/2/12 Lecture 6. Functions 2 types: – Built in functions – Custom defined functions Functions minimize the amount of repetition of code. function consists.
PHP. What is PHP? PHP stands for PHP: Hypertext Preprocessor PHP is a server-side scripting language, like ASP PHP scripts are executed on the server.
1Computer Sciences Department Princess Nourah bint Abdulrahman University.
What is PHP? PHP stands for PHP: Hypertext Preprocessor PHP is a server-side scripting language, like ASP PHP scripts are executed on the server PHP supports.
CPS120: Introduction to Computer Science Lecture 14 Functions.
Algorithms  Problem: Write pseudocode for a program that keeps asking the user to input integers until the user enters zero, and then determines and outputs.
COMPUTER PROGRAMMING. Functions’ review What is a function? A function is a group of statements that is executed when it is called from some point of.
PHP Function. Outline o What Is Function ? o Create Function o Call Function o Parameters Functions o Function Returning Values o Passing by Reference.
PHP. PHP User Defined Functions Besides the built-in PHP functions, we can create our own functions. A function is a block of statements that can be used.
PART 1 Part 1: The Material. Whether you knew it or not, all the programming that you have performed until now was in the boundaries of procedural programming.
הרצאה 4. עיבוד של דף אינטרנט דינמי מתוך Murach’s PHP and MySQL by Joel Murach and Ray Harris.  דף אינטרנט דינמי משתנה עפ " י הרצת קוד על השרת, יכול להשתנות.
Classes COMPSCI 105 SS 2015 Principles of Computer Science.
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.
IS2803 Developing Multimedia Applications for Business (Part 2) Lecture 3: Working with PHP Rob Gleasure
PHY 107 – Programming For Science. Today’s Goal  Discuss how to hand data to functions  Review loopholes in variables & scoping rules  Ways to get.
PHP Reusing Code and Writing Functions 1. Function = a self-contained module of code that: Declares a calling interface – prototype! Performs some task.
PHP Form Processing * referenced from
NMD202 Web Scripting Week2. Web site
PHP Functions / Modularity Dr. Charles Severance
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,
Arrays and Loops. Learning Objectives By the end of this lecture, you should be able to: – Understand what a loop is – Appreciate the need for loops and.
Passing Function Arguments by Reference : A Sample Lesson for CS 1 using the C Programming Language Andy D. Digh Friday, May 29th, 1998.
Introduction to Javascript. What is javascript?  The most popular web scripting language in the world  Used to produce rich thin client web applications.
Class07 PHP: loops and includes MIS 3501, Fall 2015 Brad Greenwood, PhD MBA Department of MIS Fox School of Business Temple University 9/15/2015.
COSC 1223 Computer Science Concepts I Joe Bryan. What is a function? Like a mini-program that performs a specific task Two types: Built-in functions Functions.
PHP Tutorial. What is PHP PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages.
COMPUTER PROGRAMMING Year 9 – lesson 1. Objective and Outcome Teaching Objective We are going to look at how to construct a computer program. We will.
 The real power of PHP comes from its functions; it has more than 1000 built-in functions.  PHP User Defined Functions  Besides the built-in PHP functions,
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.
CSC 1010 Programming for All Lecture 5 Functions Some material based on material from Marty Stepp, Instructor, University of Washington.
1-6 Midterm Review.
Classes, Objects and Methods
Introduction to Computer Science
ITM 352 Functions.
def-ining a function A function as an execution control structure
Introduction to scripting
Presentation transcript:

Faculty of Sciences and Social Sciences HOPE Functions in PHP Stewart Blakeway FML

Faculty of Sciences and Social Sciences HOPE Session Aims – How to define a function – How to call a function – Passing and Receiving Values to/from functions – Function Variables and Global Variables – Giving a function storage – Functions within Functions – Checking a function exists before calling it

Faculty of Sciences and Social Sciences HOPE What is a function? A function is a self-contained block of code that can be used time and time again. Values can be passed to functions. Functions, once finished executing can pass values back to the calling code.

Faculty of Sciences and Social Sciences HOPE Types of function There are two types of function. You have seen one type already and may or may not of known it was a function. – Built-in functions – Self defined functions

Faculty of Sciences and Social Sciences HOPE Built-in Functions echo (“Hello World!”); echo is the name of the function that you are calling. Hello World! is a string that we are passing to the function. Information that you want to pass to the function should be between the parentheses. How many built in functions are there?

Faculty of Sciences and Social Sciences HOPE Arguments! The terminology that explains the data that is to be passed to the function is called an argument. Some functions require more than one argument. my_function ($an_argument, $another_argument);

Faculty of Sciences and Social Sciences HOPE date () Date () is another example of a built in function. Lets examine how this works. $today = date ("l j F" ); echo $today; date () uses lots of symbols to represent different formats. ie: M,F,m,n,d,j,l,D,w,Y …

Faculty of Sciences and Social Sciences HOPE date () $today = date ("l j F" ); echo $today; Functions that return a value do not necessarily have to be stored. echo (date ("l j F" ));

Faculty of Sciences and Social Sciences HOPE date () $today = date ("l j F" ); echo $today; What data type is $today ? How could we find out? $today = date ("l j F" ); echo $today; echo (gettype($today));

Faculty of Sciences and Social Sciences HOPE Function function function echo (gettype(date("l j F" ))); What do you think is echo ’ d ?

Faculty of Sciences and Social Sciences HOPE Defining a function You can define a function by using the function statement function function_name ($argument1, $argument2 …) { // function code goes here }

Faculty of Sciences and Social Sciences HOPE Defining a function What is echoed out? <?php function bigfont($txt) { echo(" ". $txt. " "); } bigfont ("Hello World!"); ?>

Faculty of Sciences and Social Sciences HOPE tt function Examine the following code, what does it echo out? function tt($num) { for ( $x=1 ; $x<13 ; $x++ ) { echo ($x. “*”. $num. “=”. $x*$num.“ ”); } tt (6);

Faculty of Sciences and Social Sciences HOPE More about variables! Local Variables <?php $life=42; function meaningOfLife () { echo (“The meaning of life is”. $life); } meaningOfLife(); ?>

Faculty of Sciences and Social Sciences HOPE More about variables! Global Variables <?php $life=42; function meaningOfLife () { global $life; echo (“The meaning of life is”. $life); } meaningOfLife(); ?>

Faculty of Sciences and Social Sciences HOPE Alternatively Local Variables <?php $life=42; function meaningOfLife ($arg) { echo ("The meaning of life is". $arg); } meaningOfLife($life); ?>

Faculty of Sciences and Social Sciences HOPE Question Why not always pass variables to functions and do away with global variables? Sometimes you will want to remember a value of a variable even if a function has finished or manipulate a variable outside of a function

Faculty of Sciences and Social Sciences HOPE Numbered Headings <?php $numOfHeading=1; function numHeading ($heading) { global $numOfHeading; echo (" ". $numOfHeading. ". ". $heading. " " ); $numOfHeading++; } numHeading ("About Me!"); echo (" Stewart Blakeway "); numHeading ("Currently Teaching On"); echo (" Computing Concepts Database Technology IT Business Applications Web Development Network & Operating Systems "); ?>

Faculty of Sciences and Social Sciences HOPE Question Although the last example got the point across, Global variables should be avoided and only used as the last resort! Why do you think this is? The beauty of functions is their versatility, take the echo function as an example. This is a function of code that can be used again and again. Global variables hinder the flexibility that functions offer!

Faculty of Sciences and Social Sciences HOPE Numbered Headings Alternative <?php function numHeading ($heading) { static $numOfHeading = 1; echo (" ". $numOfHeading. ". ". $heading. " " ); $numOfHeading++; } numHeading ("About Me!"); echo (" Stewart Blakeway "); numHeading ("Currently Teaching On"); echo (" Computing Concepts Database Technology IT Business Applications Web Development Network & Operating Systems "); ?>

Faculty of Sciences and Social Sciences HOPE More than One Argument So far we have seen functions that pass one argument PHP can pass as many arguments as you define The results of an argument can be returned

Faculty of Sciences and Social Sciences HOPE Returning a result <?php function mySum($num1, $num2) { $total = $num1 + $num2; return $total; } $myNumber = 0; $myNumber = mySum(3, 4); echo $myNumber; ?> We saw this in slides 7 and 8

Faculty of Sciences and Social Sciences HOPE Passing Two Arguments <?php function nSFont ($txt, $size) { echo (" $txt "); } nSFont ("A heading",12); nSFont ("some body text",3); nSFont ("some more body text",3); ?>

Faculty of Sciences and Social Sciences HOPE <?php function nSFont ($txt, $size) { echo (" $txt "); } nSFont ("A heading“,12); nSFont ("some body text"); nSFont ("some more body text"); ?> What do you suppose would happen if the function was set to accept two arguments and only one was passed? A heading Warning: Missing argument 2 for nsfont() in C:\Program Files\Apache Group\Apache2\htdocs\test.php on line 6 some body text Warning: Missing argument 2 for nsfont() in C:\Program Files\Apache Group\Apache2\htdocs\test.php on line 6 some more body text

Faculty of Sciences and Social Sciences HOPE Default Arguments Local Variables <?php function nSFont ($txt, $size = 3) { echo (" $txt "); } nSFont ("A heading",12); nSFont ("some body text"); nSFont ("some more body text"); ?>

Faculty of Sciences and Social Sciences HOPE What will be echoed out? Local Variables <?php function squared($num) { $num = ($num * $num); echo ($num." "); } $num = 30; squared ($num); echo ($num." "); ?>

Faculty of Sciences and Social Sciences HOPE What will be echoed out? Local Variables <?php function squared (&$num) { $num = ($num * $num); echo ($num." "); } $num = 30; squared ($num); echo ($num." "); ?>

Faculty of Sciences and Social Sciences HOPE What will be echoed out? Local Variables <?php function squared (&$joe) { $joe = ($joe * $joe); echo ($joe." "); } $num = 30; squared ($num); echo ($num." "); ?>

Faculty of Sciences and Social Sciences HOPE Checking a function exists <?php function mySum($num1, $num2) { $total = $num1 + $num2; return $total; } if (function_exists (mysum)) { $myNumber = mySum(3, 4); echo $myNumber; } else { echo ("Function not available"); } ?>

Faculty of Sciences and Social Sciences HOPE What next? Seminar – More exercises, specifically defining functions and calling functions Next Lecture – This is on arrays, associate arrays, multidimensional arrays, classes, methods.. And more … don ’ t miss this as its not easy!

Faculty of Sciences and Social Sciences HOPE tt function – 3 Errors function tt($num) for ( $x=1 ; $x<13 ; $x++ ) { echo (x. “*”. $num. “=”. $x*$num. “ ”); } tt 6;

Faculty of Sciences and Social Sciences HOPE 3 Errors Global Variables <?php $life=42; function meaningOfLife () { Global $life; echo (“The meaning of life is” $life); } meaningOfLife(); >

Faculty of Sciences and Social Sciences HOPE Numbered Headings – 3 Errors <?php function numHeading($heading) { fixed $numOfHeading = 1; echo (" ". $numOfHeading. ". ". $heading. " " ); $numOfHeading++; } numHeading ("About Me!"); echo (" Stewart Blakeway "); numheading ("Currently Teaching On"); echo (" Computing Concepts Database Technology IT Business Applications Web Development Network & Operating Systems "); ?>

Faculty of Sciences and Social Sciences HOPE Any Questions?