Functions
Main | Static vs. Dynamic Websites | Using PHP | A Simple PHP Program | Variables and Operators | Practice 1 | Solution 1 | Loops | Practice 2 | Solution 2 | Functions | Practice 3 | Solution 3 | Conditionals | Practice 4 | Solution 4 In English the word modular, is defined as "of or consisting of modules", and the word module is defined as "a standardized part or independent unit used in construction". The ideas in these definitions can serve as a powerful programming paradigm, or method of thought. In the construction of PHP programs, a module can be thought of as a piece of working code, that does a specific job. We can use a modular approach to program design to organize our code. Instead of writing one large program, we will write many small programs and then put them together. A large PHP program can then be built like a large building, "one brick at a time". A complex PHP program can then be seen as a set of smaller programs, and if each of those smaller programs can be understood, then the entire large program will be understood. To implement a modular programming paradigm, PHP allows us to define functions. We will now examine how to use functions in PHP. We often write code that we want to reuse later in the same program. The declaration of functions allows us to do this in an organized and elegant way. The details of function declaration will be covered soon, but the general idea is that you write a piece of code and give that piece of code a name, and then whenever you need to use that piece of code, you just use the name that you gave it. Hence, instead of cutting and pasting a few lines of code that we will need to use again and again, we could define those few lines of code in something called a function, which we could insert into our program with one line. Then, whenever we need to use those few lines of code again, we simply "call the function". Recall our first PHP program. <html> <h3>My first PHP Program</h3> <? echo "Hello world!"; ?> </html>This code can be varied to produce a similar program using functions. When declaring a function, you must first state "function" and then, after inserting a space, follow with one word followed immediately by a set of parentheses. To review, hello_world would be considered only one word by PHP, since "hello" and "world" are separated by an underscore ( _ ), and there are no spaces between "hello" and "world". You must then open up a set of curly braces ({ and }), and whatever PHP code you write inside of those curly braces will implement your function. Here is what the above description looks like: function your_function_name() { your PHP code } In PHP3 A program which uses functions, must first start with a definition of the functions it will use, and only then may those functions be called upon. No such requirement exists in PHP 4. To see an example of defining and then using a function, observe the following, in which we first define a function that echoes the "Hello world!" message, and then calls that function after it has been declared. <html> <? function hello_world() { echo "Hello world!"; } ?> <h3>A Modular PHP Program</h3> <? echo "I am about to say hello..."; hello_world(); echo "There you go, I said something."; ?> </html>You can run the above by clicking here. You should notice how this is similar to our first PHP program, and observe that it only differs in how it is written as a set of modules. One module is a function that echoes a message, and the other module calls the function. Functions can be passed arguments. An argument can be thought of as an input to a function. To give a function an argument, we define the function with a variable that is in-between its set of parentheses. To give an example of this we will consider a variation of the PHP program above. Suppose that in the above example, we did not know what we wanted to echo. We could redefine the function hello_world() so that it is more generic and says "hello" to whomever we specify when we call the function, like the following. function hello($someone) { echo "Hello $someone!"; }The variable $someone is defined when we call the function. If we wanted the exact same output as in the program above, our function call would look like the following. hello(world);In the above the string "world" is being passed as an argument, but we could pass any string we like ( hello("Richard Stallman") , hello("Linus Torvalds"), etc. ). A rewritten version of the above that uses functions that pass arguments would be the following. <html> <? function hello($something) { echo "Hello $something!"; } ?> <h3>A Modular PHP Program</h3> <? echo "I am about to say hello..."; hello(world); echo "There you go, I said something."; ?> </html>You can run the above by clicking here. Suppose however, that no argument was passed in the function hello(). In other words, suppose that you changed a portion of the code above, so that it looked like the following: echo "I am about to say hello..."; hello(); echo "There you go, I said something."; You would get an error having a resemblance to: Warning: Missing argument 1 in call to hello() in /home/staff/jfulton/public_html/php1/modular_arg.php on line 3 If we want to avoid seeing the above error message in the cases that our hello() function is called without an argument, we can redefine as such: function hello($something="") { echo "Hello $something!"; } In the above, the variable $something has an initialization set to an empty string. Since we normally represent a string in PHP by putting quotes (") around it, a quote followed immediately by another quote (""), denotes a string with no content (known formally as the null-string, which is similar to the number zero). If the function hello() is called without an argument, the variable $something is set to the empty string and there will be no error message, but if the function hello() is called with an argument, the function will have the same behavior as it did when we defined it above. You can pass sentences to functions as well. In addition to functions that you may define, PHP comes with its own library of built-in functions. These functions are a part of PHP, and you don't have to define them if you use them. These functions have already been written. To use them you only need to know is what the function does, and if it takes an argument. You may find that a lot of the things you are interested in programming are already almost done for you. An index of built-in PHP functions is available online at: http://www.php.net/manual/en/funcref.php Functions can be used to implement a modular approach to programming. As a review, remember that a modular approach allows a program to be simplified, because it can be reduced to a set of smaller programs, or modules. Thus, if each function in a large program can be understood, the entire program can be understood. Nearly all of our future Practices will build upon a modular approach, which Practice 3 will emphasize.
jfulton [at] member.fsf.org
|