Creating PHP Pages Chapter 8 PHP Functions PHP

  • Slides: 10
Download presentation
Creating PHP Pages Chapter 8 PHP Functions

Creating PHP Pages Chapter 8 PHP Functions

PHP Functions • Functions are groups of statements that you can execute as a

PHP Functions • Functions are groups of statements that you can execute as a single unit • PHP functions need to be defined with keyword function • It can have zero or more values (parameters) • Functions may or may not return values • If a function need to return value, the last statement of the function should be return value; • The set of curly braces (called function braces) contain the function statements

PHP Functions • Function declaration in PHP <? php function name_of_function(parameters) { statements; }

PHP Functions • Function declaration in PHP <? php function name_of_function(parameters) { statements; } ? > for e. g. function say. Hello() { echo(“<b>hello<b> ”); }

PHP Functions • Parameter less function <? php function hello() { echo “hi”; }

PHP Functions • Parameter less function <? php function hello() { echo “hi”; } ? > • This can be called as <? php hello(); ? > in the program

PHP Functions • Parameterized function <? php function greet($name) { echo “Hello “. $name;

PHP Functions • Parameterized function <? php function greet($name) { echo “Hello “. $name; } ? > • This can be called <? php greet(‘You’); ? > This gives an output Hello You

PHP Functions • Assigning functions to the variables for e. g $first = “first_func”;

PHP Functions • Assigning functions to the variables for e. g $first = “first_func”; to invoke the function first_func() through the variable $first(); • When an argument is to be passed by reference, an ampersand (&) is placed before the parameter name for e. g. first_func(&$first_ref);

Returning Values • A return statement returns a value to the statement that called

Returning Values • A return statement returns a value to the statement that called the function • Not all functions return values function average. Numbers($a, $b, $c) { $Sum. Of. Numbers = $a + $b + $c; $Result = $Sum. Of. Numbers / 3; return $Result; }

Returning Values • You can pass a function parameter by value or by reference

Returning Values • You can pass a function parameter by value or by reference • 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.

global Keyword • In PHP, you must declare a global variable with the global

global Keyword • In PHP, you must declare a global variable with the global keyword inside a function definition to make the variable available within the scope of that function • Example : <? php $Global. Variable = "Global variable"; function scope. Example() { global $Global. Variable; echo "<p>$Global. Variable</p>"; } scope. Example(); ? >

References Thank’s to : Blazzard, J. B. (2011). Functions and Control Structures (ppt document).

References Thank’s to : Blazzard, J. B. (2011). Functions and Control Structures (ppt document). Retrieved from Lecture Notes Online Web site: http: //www. lcsc. edu/jblazzar/citpt 227/. References : 1. 2. 3. 4. Anonymous. (n. d. ). Apache HTTP Server Documentation Version 2. 2. Retrieved from http: //httpd. apache. org/docs/2. 2/. Achour, M. , Betz, F. (n. d. ), PHP Manual. Retrieved from http: //www. php. net/download-docs. php. Anonymous. (n. d. ). My. SQL Reference Manual. Retrieved from http: //downloads. mysql. com/docs/. Naramore, E. , Gerner, J. , Le Scouarnec, Y. , Stolz, J. , Glass, M. K. (2005). Beginning PHP 5, Apache, and My. SQL® Web Development. Indianapolis, IN: Wiley Publishing, Inc.