![]()
Software
Books
Each book purchase gives a couple bucks back to the PHP Development Team!
And there you have it. Because I specifically declared $REMOTE_ADDR to be a global variable
inside this function, I can now see it and echo it as if it were a local variable. I cannot
have access to global variables like $HTTP_USER_AGENT in that function, since I did not
declare it.
To call this function, I simply say:
And it will echo:
PHPBuilder.com
1
127.0.0.1
I use functions for commonly-used code in my pages. It's cleaner than simply using include to include the
same code over and over, although both will work. One very interesting use for functions is to build common features
on an HTML page, like your headers/footers.
There's a whole lot more that you can do with functions, and I won't mention it all here. This should be enough
to get you up and running.
Quick How-To's
Getting Started With Functions
Tim Perdue
Functions are interesting animals in PHP. Functions are kind of like "black boxes"
since they cannot see outside of their universe and you cannot see inside of them. That's
a bit different than some other languages I've programmed in. In some languages, a function would
be able to see "global variables", but in PHP they cannot unless you specifically set it up that way.
function my_function ($string, $int) {
global $REMOTE_ADDR;
echo $string."<P>";
echo $int."<P>";
echo $REMOTE_ADDR;
}
my_function ("PHPBuilder.com", 1);
function site_header ($title) {
?>
<HTML><HEAD><TITLE><?php echo $title; ?></TITLE></HEAD>
<BODY>
...more HTML....
<?php
}
function site_footer() {
?>
...more HTML...
</BODY></HTML>
<?php
}

