Servage Magazine

Information about YOUR hosting company – where we give you a clear picture of what we think and do!

Functions and commands in PHP – Part 2

Thursday, June 12th, 2014 by Servage


(Continued from part 1)

To output today’s date using this function, place the following call in your code:

echo longdate(time());

This call uses the built-in PHP time function to fetch the current Unix timestamp and passes it to the new longdate function, which then returns the appropriate string to the echo command for display. If you need to print out the date 17 days ago, you now just have to issue the following call:

echo longdate(time() - 17 * 24 * 60 * 60);

which passes to longdate the current Unix timestamp less the number of seconds since 17 days ago (17 days × 24 hours × 60 minutes × 60 seconds).

Functions can also accept multiple parameters and return multiple results, using techniques that I’ll introduce over the following chapters.

Variable Scope

If you have a very long program, it’s quite possible that you could start to run out of good variable names, but with PHP you can decide the scope of a variable. In other words, you can, for example, tell it that you want the variable $temp to be used only inside a particular function and to forget it was ever used when the function returns.

In fact, this is the default scope for PHP variables. Alternatively, you could inform PHP that a variable is global in scope and thus can be accessed by every other part of your program.

Local variables

Local variables are variables that are created within, and can be accessed only by, a function. They are generally temporary variables that are used to store partially processed results prior to the function’s return.

One set of local variables is the list of arguments to a function. In the previous section, we defined a function that accepted a parameter named $timestamp. This is meaningful only in the body of the function; you can’t get or set its value outside the function.

For another example of a local variable, take another look at the longdate function, which is modified slightly.

An expanded version of the longdate function

function longdate($timestamp)
$temp = date("l F jS Y", $timestamp);
return "The date is $temp";

Here we have assigned the value returned by the date function to the temporary variable $temp, which is then inserted into the string returned by the function. As soon as the function returns, the value of $temp is cleared, as if it had never been used at all.

Now, to see the effects of variable scope, let’s look at some similar code. Here, $temp has been created before calling the longdate function.

This attempt to access $temp in the function longdate will fail:

$temp = "The date is ";
echo longdate(time());
function longdate($timestamp)
return $temp . date("l F jS Y", $timestamp);

Because $temp was neither created within the longdate function nor passed to it as a parameter, longdate cannot access it. Therefore, this code snippet only puts out the date and not the preceding text. In fact, it will first display the error message “Notice: Undefined variable: temp.”

The reason for this is that, by default, variables created within a function are local to that function and variables created outside of any functions can be accessed only by non-function code.


Sources for further reading

Functions and commands in PHP - Part 2, 5.0 out of 5 based on 3 ratings
Categories: Guides & Tutorials, Tips & Tricks


You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

No comments yet (leave a comment)

You are welcome to initiate a conversation about this blog entry.

Leave a comment

You must be logged in to post a comment.