Home and Learn: PHP Programming Course


By Ref, By Val

 

Functions can be quite hard to get used, if you've never met them before. Another difficult part to understand is how values can change, or not change, depending on scope. Scope, if you recall, refers to where in your code a variable can be seen. If you just do this, for example:

$Variable_Value = 10;
example( );

function example() {

print $Variable_Value;

}

then you'll get a PHP error about "undefined variable". That's because the function called example( ) can't see what's inside of the variable called $Variable_Value.

In order for the function to be able to see what’s inside of the variable called $Variable_Value, you can set up the function to accept an argument. You'd then type the variable name between the round brackets, when you come to call it. Like this:

<?PHP

$Variable_Value = 10;
example($Variable_Value);

function example($Variable_Value) {

print $Variable_Value;

}

?>

If you run the code above, it now prints out the number ten. But it's important to bear in mind that you are just handing the function a copy of the variable. You're not effecting the original. As an example, change your code to this:

<?PHP

$Variable_Value = 10;

print "Before the function call = " . $Variable_Value . "<BR>";

example($Variable_Value);

print "After the function call = " . $Variable_Value;

function example($Variable_Value) {

$Variable_Value = $Variable_Value + 10;
print "Inside of the function = " . $Variable_Value . "<BR>";

}

?>

Here, we have three print statement: one before the call to the function, one inside of the function, and one after the function call. But we're printing out the value of the variable called $Variable_Value each time. Inside of the function, we're adding ten to the value of the variable. When you run the code, it will print out this:

Before the function call = 10
Inside of the function = 20
After the function call = 10

The important one is After the function call. Even though we changed the value of $Variable_Value inside of the function, it still print 10 after the function call! That's because the function was handed a copy, and NOT the original.

When you hand a function a copy of a variable, it's called passing the variable by value (just a copy). The alternative is to NOT pass a copy, but to refer back to the original. Make one small change to your script. This:

function example(&$Variable_Value) {

The only addition is a & character before the variable between round brackets. This tells PHP that you want to make changes to the original, and don't just want a copy. When you run the script, it now print out the following:

Before the function call = 10
Inside of the function = 20
After the function call = 20

After the function call, we now have a value of 20! So a change to the value of the variable outside the function has been made. When you makes changes to the original like this, it's called passing the variable by reference (don't just copy it – remember it).

Try not to worry about value and reference. Unless the answers you're getting back from your function are rather odd, that is!


In the next part, we'll take a look at Server Variables.

<-- Back One Page | Move on to the Next Part -->

Back to the PHP Contents Page

 

Email us: enquiry at homeandlearn.co.uk