This is the last step in understanding how variables, arrays, and functions are similar, are different, and come together to form the basis of the PHP universe.
So far in this series, we have covered variables and arrays. Granted, it took us three months to cover arrays and how to display them, but let's not worry about that now.
In the process, we saw that variables were preceded by a dollar sign ($) and consisted of underscores plus any alphanumeric digits (that is, no special characters) as long as the first character after the $ was an underscore or alpha. We saw that we did not have to specify what the data type was or what the field looked like. Instead, that was determined by the type of data we put in that variable. And speaking of data types, we saw that a PHP variable could be an integer, string, floating point, object, resource, Boolean, or array.
And speaking of arrays, we saw that an array is a variable (so it starts with a $) and that there are really two types of arrays: numeric and associative.
Numeric arrays are the kind of array that we are most familiar with in RPG. That is, they have a numeric index, and the difference between RPG and PHP is that the index in RPG starts with 1, and in PHP (which is derived from C) it starts with 0.
The second type of array is the associative array. In this case, we can make the index not just a number but a string. That means we can have an array in which the index is 'name', 'Address1', 'City', etc. Obviously, this gives us a whole new dimension and allows us to keep file-like structures within the PHP script, which is very useful when reading records from a website or writing records to it. We saw that this process (using the foreach operator) was not trivial but neither was it rocket science.
There is one more entity that we should look at to round out all of this stuff, however, and that is functions.
Functions
In one sense, a function is like an RPG subroutine. Although to be honest, it is actually much more like an RPG subprocedure. What's the difference? A number of things, of course, but the one I'm talking about mostly is how they each handle variable scope. Subroutines have no concept of variable scope. There are no local variables; everything is a global variable. But subprocedures have local and global variables. Hang on to that thought; we'll return to it eventually.
Functions in PHP are a way of packaging capability, sometimes one function but generally more than one into a neat little package that can be run as a unit. By themselves, functions are not object-oriented, but they're important building block of the classes that do define OO.
Function names are not preceded by a $ (or any special character) like variables are, but they do consist of alphanumerics and underscores. Certain special functions, ones that are a default part of PHP, start with two underscores and are called "magic methods." But most of the functions you use will be ones that you define yourself. Probably. Actually, I have no idea what kinds of things you're likely to do in the future. No clue. But that's not my problem, so let's just move on.
Defining Functions
You will remember that we defined variables by simply assigning a value to the name, such as $name = 'Dave';. Functions, on the other hand, are defined by using the special "function" keyword.
Function Write_Msg() {
echo "Write a Message";
}
Let's dissect this. "Function" is the keyword that lets PHP know you're creating a function. Duh. Write_Msg is the name of the function. The parentheses are required; later we'll stick some parameters in there. Finally, the braces are also required; they enclose the statements that are to be carried out when we run the function. In this case, all we're going to do is display the message Write a Message on the screen. Also, please note that there's a semicolon after each of the statements enclosed in braces, but none, that's right, none at the end of the function definition.
Calling or Running a Function
So if that's how to define a function, how do you get it to run? Simple.
Write_Msg();
That's right. That's all there is to it. If you ran this function by calling the URL for the script that contains the function definition, the following would display on the web page.
Write a Message
What I'm saying is that if you wrote and ran the following script,
<?php
Function Write_Msg() {
echo "Write a Message";
}
Write_Msg();
?>
you would see Write a Message displayed on the web page.
Functions with Parameters
In the above example, we didn't put anything in the parentheses when we defined the function. But you could put parameters in there and pass them into the function.
<?php
Function Add($num1, $num2) {
$sum = $num1 + $num2;
echo $sum;
}
$num1 = 3;
$num2 = 5;
Add($num1, $num2);
?>
The above script defines the Add function, sets the initial values of the two parms, and then carries out the add and display that are part of the function definition. If you run this script, you'll get the result of 8. Notice that you don't need to define the variables before you use them as parameters, but you must set values before you run the function.
I would be remiss if I didn't point out that in the wild (that is, in most of the PHP programs you will see), the function is executed by setting it equal to a variable, just like the way we can call programs in /free by using an eval statement. So, the above could be written as
$ans = Add($num1, $num2);
echo $ans;
and the result of 8 would be displayed. Note that $ans isn't defined anywhere previously; it just sort of creates itself out of nothing, much like xxx.
Variable Scope
One of the big lessons we can take out of this is to learn something about variable scope. If you're doing just regular RPG, then all of the variables in a program are global variables. Doesn't matter if they're used in just one subroutine or not; they're global variables that are defined in F- or D-specs at the start of the program.
If, however, you're using subprocedures in your RPG program, you know that you can also define local variables (using D-specs within the subprocedure) that are valid in the subprocedure but are not available in the rest of the program.
PHP also has both global and local variables, and you're really encouraged in PHP to use local variables, not global. This becomes easier in OO PHP, where you can define classes consisting of both variables and functions, but it's a good idea for procedural PHP also.
For example, if we have this script
<?php
$num1 = 3;
$num2 = 5;
Function Add() {
$sum = $num1 + $num2;
echo $sum;
}
Add();
?>
we would get a result of 0 rather than 8. Why? Because the variables in this script are global variables; they're defined outside of the function, and hence they don't work in the function.
And this is one of the purposes of the parameters. If we instead write the function as
Function Add($num1, $num2) {
$sum = $num1 + $num2;
echo $sum;
}
Add($num1, $num2);
then the result would be 8. The use of parms would allow us to bring the global variables in and use them in the function (as we did above).
Summary
So is that it? Are functions really that simple? Well, yes, sort of. Remember, the real complexity of the function is the set of statements that you put in it. But there's more to learn about functions: setting up default parms, passing parms by reference or value, using dynamic function names, etc. But I will leave that up to you. Take care.
LATEST COMMENTS
MC Press Online