100 likes | 246 Views
Creating PHP Pages. Chapter 8 PHP Functions. 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
E N D
Creating PHP Pages Chapter 8 PHP Functions
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 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; } ?> for e.g. function sayHello() { echo(“<b>hello<b><br />”); }
PHP Functions Parameter less function <?php function hello() { echo “hi”; } ?> This can be called as <?phphello(); ?> in the program
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”; 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 the function Not all functions return values function averageNumbers($a, $b, $c) { $SumOfNumbers = $a + $b + $c; $Result = $SumOfNumbers / 3; return $Result; }
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 globalkeyword inside a function definition to make the variable available within the scope of that function Example : <?php $GlobalVariable = "Global variable"; function scopeExample() { global $GlobalVariable; echo "<p>$GlobalVariable</p>"; } scopeExample(); ?>
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/citpt227/. References : 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.). MySQL Reference Manual. Retrieved from http://downloads.mysql.com/docs/. Naramore, E., Gerner, J., Le Scouarnec, Y., Stolz, J., Glass, M. K. (2005). Beginning PHP5, Apache, and MySQL® Web Development. Indianapolis, IN: Wiley Publishing, Inc.