Home Community Mys-Script Creative Off-Topic |
|
|
Thread Tools | Display Modes |
#1
|
||||
|
||||
Hall of Famer's PHP Tutorials: Chapter 5 - PHP Function Basics
Chapter V - Function Basics In chapter 4, you learned how to play with PHP's iteration/loop structure. These are the very basic contents of PHP as a programming language, and guess what? You are just one step away from turning yourself from a noncoder into a beginner programmer! To complete this step, you need the knowledge of functions. You may wonder why functions are useful, the idea is to be able to group common functionalities and reuse code. The worst practice of programming is needless repetition, keep this in mind. This tutorial will teach you how to declare functions, create functions with arguments, functions with return values/void, variable scope in functions and return function value by reference. Since there are just too many concepts involved with functions, I've decided to write two separate articles about the basics and advanced materials. The other one will be posted after the introduction of PHP arrays, strings, superglobals and mysql, before we move onto the world of object oriented programming. Note you need to understand the basics of functions in order to learn concepts like arrays, strings and so on. 1. Declare a function: To declare a function, you use the function keyword followed by the function name and parenthesis (). It sounds simple, isnt it? Now lets give this a try: PHP Code:
Supplied values are called arguments in programming languages such as PHP. Sure a PHP function can survive just fine without an argument, but in most circumstances functions do accept arguments and perform on them. So lettuce modify the function a bit so it can accept external variables as arguments: PHP Code:
PHP Code:
Our tiny little function isnt quite useful at this point, this is because we have not define any functionalities inside the function itself. To accomplish this, we need to write code in the function body. PHP can distinguish what are inside and outside of the function if you enclose lines belong to a function within curly bracket{}. An example is given below: PHP Code:
PHP Code:
Such a function above is called a void function, which performs operations but does not return any values. Void functions are useful in mysql insert, update and delete operations, since all you care about is the operation succeeds. Imagine, however, you are playing with mysql select operations in order to retrieve some database record. In this case, a function that does not return anything is clearly not what you want. You can have a function to return a single value by using the keyword return, this will stop function execution immediately and returns the value you want back to the main program: PHP Code:
We have defined our function successfully, how nice! Unfortunately, PHP does not execute such a function unless you call it in the main program or another function that is called by the main program. To call a function from main program, do: PHP Code:
Assume we do not have a value for argument $y and instead, we want it to resume the default value 0? It cannot get any simpler, we omit the second argument: PHP Code:
PHP Code:
PHP Code:
4. Variable Scope in functions: In section 2 I briefly introduced a concept called local variable, which are only available inside the function itself. In the example above, $a, $b and $c are all local variables in myfunc(), you cannot use print, echo or var_dump on these local variables from the main program. Also do not attempt to use them in further operations, this can create a fatal error. Variables defined outside of the function are also inaccessible to the function. Let's say we have a variable $v3 defined in the main program, but it is not passed as an argument to myfunc(). The value of $v3 will be unavailable in our function, operations depending on it will inevitably fail: PHP Code:
We can of course re-declare the function to accept a third argument, then pass $v3 to the third argument. This will work, although a bit tedious if we have many functions depending on too many arguments. Another solution is to use the global keyword on $v3, which makes a global variable. Global variables are accessible in global scope, which means anywhere inside the script, they can be convenient at times: PHP Code:
5. Pass/return values by reference: Now let's take a break from our function example and look at some interesting concept here. Usually a variable is a unique identity whose values do not get changed when another variable changes/reassigns. Consider the following example below: PHP Code:
PHP Code:
This idea of reference can be extended to the usage of functions. You can pass variables by reference instead of by value. A good example is shown here: PHP Code:
We can also return from a function by reference. This is a bit tricky, the below program demonstrates the point of returning by reference: PHP Code:
You may wonder why we need to pass or return by reference. Well let's say we have several mysql void functions to execute, and we wish to keep track of the state of the mysql driver with a variable. Without passing/returning by reference, the variable we pass to mysql functions will never be able to record the state, since what we get from the end is the original value itself. In fact, this is better accomplished by using objects, which I will be writing lots of articles to describe what the heck they are. For now, just remember objects are reference types, they pass and return as reference by default. For this reason, theres no need to use cumbersome ampersand symbols if we use objects! Alright, guess that's it for this tutorial. You must have been exhausted with this many weird concepts, havent you? PHP function is a subject that may requires weeks or even months to digest, even now there are still highly complex concepts of functions beyond my interpretation. Dont worry, we aint gonna reach that far in my tutorial about advanced functions. I will be writing about anonymous functions, recursive functions, variable functions and a brief overview of PHP's built-in functions in the tutorial that follows Arrays and Strings. They aint that hard to comprehend so long as you understand the basics of functions very well. Until then, I hope you enjoy the latest release of Mysidia Adoptables, and have fun reading the tutorials I write. Take care.
__________________
Mysidia Adoptables, a free and ever-improving script for aspiring adoptables/pets site. |
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Hall of Famer's PHP Tutorials: Chapter 6 - PHP Arrays | Hall of Famer | Tutorials and Tips | 0 | 03-16-2013 08:14 PM |
Hall of Famer's PHP Tutorials: Chapter 1 - PHP syntax | Hall of Famer | Tutorials and Tips | 10 | 02-02-2012 11:31 AM |
Hall of Famer's PHP Tutorials: Chapter 4 - PHP Iteration | Hall of Famer | Tutorials and Tips | 2 | 02-26-2011 10:48 AM |
Hall of Famer's PHP Tutorials: Chapter 3 - PHP Conditional Statements | Hall of Famer | Tutorials and Tips | 0 | 02-23-2011 06:59 PM |
Hall of Famer's PHP Tutorials: Chapter 2 - PHP Operators | Hall of Famer | Tutorials and Tips | 0 | 02-22-2011 08:26 PM |
What's New? |
What's Hot? |
What's Popular? |