Presentation is loading. Please wait.

Presentation is loading. Please wait.

PHP Functions, Scope MIS 3501, Fall 2015 Jeremy Shafer

Similar presentations


Presentation on theme: "PHP Functions, Scope MIS 3501, Fall 2015 Jeremy Shafer"— Presentation transcript:

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

2 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; } ?>

3 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

4 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>"; }

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

6 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");

7 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.

8 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.

9 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.

10 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

11 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.


Download ppt "PHP Functions, Scope MIS 3501, Fall 2015 Jeremy Shafer"

Similar presentations


Ads by Google