PHP Functions, Scope MIS 3501, Fall 2015 Jeremy Shafer

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Pass by Value. COMP104 Pass by Value / Slide 2 Passing Parameters by Value * A function returns a single result (assuming the function is not a void function)
Introduction to PHP MIS 3501, Fall 2014 Jeremy Shafer
Objectives Using functions to organize PHP code
PHP Conditions MIS 3501, Fall 2014 Jeremy Shafer Department of MIS Fox School of Business Temple University September 11, 2014.
PHP Functions and Control Structures. 2 Defining Functions Functions are groups of statements that you can execute as a single unit Function definitions.
Chapter 4 Functions and Control Structures PHP Programming with MySQL.
CSCI 116 Functions.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Functions.
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.
PHP Programming with MySQL Slide 4-1 CHAPTER 4 Functions and Control Structures.
WDMD 170 – UW Stevens Point 1 WDMD 170 Internet Languages eLesson: Variables, Functions and Events (there is an audio component to this eLesson) © Dr.
Chapter 2 Functions and Control Structures PHP Programming with MySQL 2 nd Edition.
CPS120: Introduction to Computer Science Functions.
Learners Support Publications Functions in C++
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
JavaScript, Fourth Edition
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.
Week 8 - Friday.  What did we talk about last time?  Static methods.
Fall 2004CSI University of Ottawa Introduction to PHP Basic principles and syntax.
Creating FunctionstMyn1 Creating Functions Function can be divided into two groups: –Internal (built in) functions –User-defined functions.
Introduction to JavaScript MIS 3502, Spring 2016 Jeremy Shafer Department of MIS Fox School of Business Temple University 2/2/2016.
Tarik Booker CS 242. What we will cover…  Functions  Function Syntax  Local Variables  Global Variables  The Scope of Variables  Making Functions.
 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,
1 Sections 6.4 – 6.5 Methods and Variables Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Sessions and cookies MIS 3501, Spring 2016 Jeremy Shafer Department of MIS Fox School of Business Temple University 4/12/2016.
PDO Database Connections MIS 3501, Fall 2016 Jeremy Shafer Department of MIS Fox School of Business Temple University 3/8/2016.
Introduction to JavaScript MIS 3502, Fall 2016 Jeremy Shafer Department of MIS Fox School of Business Temple University 9/29/2016.
PDO Database Connections
Sessions and cookies MIS 3501 Jeremy Shafer Department of MIS
Form Data (part 2) MIS 3502, Fall 2015 Jeremy Shafer Department of MIS
Classes and Objects.
C Functions -Continue…-.
PHP 5 Syntax.
jQuery – Form Validation
Form Data (part 1) MIS 3502, Fall 2015 Jeremy Shafer Department of MIS
Functions Comp 205 Fall ‘17.
Week 8 - Friday CS 121.
Modern JavaScript Develop And Design
JavaScript: Functions
Function There are two types of Function User Defined Function
PDO Database Connections
How to get data from a form
Introduction to JavaScript
A second look at JavaScript
Functions BIS1523 – Lecture 17.
PDO Database Connections
Organize your code with MVC
Sessions and cookies (part 1)
PDO Database Connections
Class07 PHP: loops MIS 3501 Jeremy Shafer Department of MIS
Class05 How to get data from a form
MySQL Backup, Transfer and Restore
PDO Revisited MIS 3502 Jeremy Shafer Department of MIS
Objectives In this chapter, you will:
Sessions and cookies MIS 3501 Jeremy Shafer Department of MIS
MCS/BCS.
Basics (Cont...).
Chapter 9: Value-Returning Functions
An Introduction to JavaScript
Programming Control Structures with JavaScript Part 2
Loops and Arrays in JavaScript
Introduction to JavaScript
MVC – Model View Controller
Introduction to C++ Programming Language
CIS 136 Building Mobile Apps
CPS125.
Scope Rules.
Presentation transcript:

PHP Functions, Scope MIS 3501, Fall 2015 Jeremy Shafer Department of MIS Fox School of Business Temple University October 22, 2015

Functions Functions are groups of statements that you can execute as a single unit Function definitions are the lines of code that make up a function The syntax for defining a function is: <?php function name_of_function(parameters) { statements; } ?>

Functions (continued) Functions, like all PHP code, must be contained within <?php ... ?> tags A parameter is a variable that is passed to a function when it is called Parameters are placed within the parentheses that follow the function name Functions do not have to contain parameters The set of curly braces { } contain the function statements

Functions (continued) Function statements do the actual work of the function and must be contained within the function braces function displayCompanyName($Company1, $Company2, $Company3) { echo "<p>" . $Company1 . "</p>"; echo "<p>" . $Company2 . "</p>"; echo "<p>" . $Company3 . "</p>"; }

Calling Functions function displayCompanyName($CompanyName) { echo "<p>" . $CompanyName . "</p>"; } displayCompanyName("Course Technology");

Returning Values A return statement returns a value to the statement that called the function Note the difference between this function and the function from the previous slide. function displayCompanyName($CompanyName) { $result = "<p>" . $CompanyName . "</p>"; return $result; } echo displayCompanyName("Course Technology");

Returning Values What are the advantages of using return? Another example of a return statement. function averageNumbers($a, $b, $c) { $SumOfNumbers = $a + $b + $c; $Result = $SumOfNumbers / 3; return $Result; } What are the advantages of using return? You should get in the habit of using return.

Parameters By default, PHP passes parameters by value. A function parameter that is passed by value is a local copy of the variable. A function parameter that is passed by reference is a reference to the original variable. In this class we will always pass parameters by value.

Some examples You can create a function that will: Render HTML for you. This allows you to reuse PHP code. (htmlfun.php) Perform “complex” mathematics. (mathfun.php) Perform “complex” string manipulation. (eliza.php) The goal of all this is to make the code easier to read, and easier to maintain.

Understanding Variable Scope The scope of a variable determines where in your program a declared variable can be used A local variable is declared inside a function and is only available within the function in which it is declared

Understanding Variable Scope The global command can be used to deliberately make a variable available inside of a function. You should use the global command sparingly! Generally it is best to pass the variables you need into the function as parameters. In our text-book examples, global is used to make a database connection variable available within the function.