You are on page 1of 15

SOFT7008

Server Side Programming


PHP-06
Functions & Parameters
Functions can modularise code
function starburst()
{
echo "*";
echo "*";
echo "*";
• A function can be used to echo "*";
package a chunk of code so that echo "*";
it can be reused
echo "*";
echo "*";
echo "*";
• In this situation it's as if the code echo "*";
was just copied and pasted echo "*";
echo "*";
echo "*";
echo "<br/>";
}
Functions can modularise code

• A function can be used to function hashburst($number)


package a chuck of code so that {
it can be reused
for ($i=1; $i<=$number; $i++)
{
• In this situation it's as if the code
echo "#";
was just copied and pasted

}
• Modularised code can user echo "<br/>";
parameters }
Functions can modularise code
function stringburst($txt,$number)
{
for ($i=1; $i<=$number; $i++)
{
echo $txt;
}
echo "<br/>";
}

• A function can have several parameters


Functions as statements

echo "Hello"
• In the code the function can appear
like a statement

stringburst("Ho", 7);

echo "Goodbye";
• It can DO something
Returning Values

function averageof2 ($num1, $num2)

• A function can return a value $answer = ($num1 + $num2)/2;

return $answer;

}
Returning Values
function averageof2 ($num1, $num2)

{
• A function can return a value

$answer = ($num1 + $num2)/2;


• A function that returns a values
doesn't do something, it becomes return $answer;
something
}

echo averageof2 (12,6) + 10;


Returning Values
function averageof2 ($num1, $num2)

{
• A function can return a value

$answer = ($num1 + $num2)/2;


• A function that returns a values
doesn't do something, it becomes return $answer;
something
} this becomes a number

echo averageof2 (12,6) + 10;


Doing or Becoming

• As a general rule a function should either do something or become


something

• It is poor practice to mix and match

• Functions that evaluate to something should not have side effects


Parmeter Scope
function averageof2 ($num1, $num2)

{
• The scope of variables declared in a
function does not extend beyond $answer = ($num1 + $num2)/2;
the function

return $answer;
• They don't exist outside the function
}

echo averageof2 (12,6) + 10;


Parmeter Scope

function oneless ($number)

{
• Changes made to parameters with
the function have no effect outside it

$number = $number -1;

• The function works with a copy of it


return $number;

}
function oneless ($number)

$number = $number -1;


• Changes made to parameters within
the function have no effect outside it
return $number;

• The function works with a copy of it


}

• Here $x is passed in as a parameter


$x = 19;

• It is changed inside the function


echo $x; 19

• But outside it is unharmed echo oneless ($x); 18

echo $x; 19
Passing Variables by Value

• When a parameter is passed to a function a copy is made of it

• Changes made to the variable inside the function have no effect on the
original variable value

• This is called passing variables by value

• This is the default in PHP


Passing Variables by Reference

• When a parameter is passed to a function it deals with the original variable

• Changes inside the function are made to the original variable

• This is called passing variables by reference

• This is can be done in PHP by using & in the function declaration

• This should be used with caution


function subtractone (&$number)

• Changes made to parameters within


$number = $number -1;
the function affect the original value
outside it

return $number;
• The function works with the original
assed variable
}

• Here $x is passed in as a parameter


$x = 19;

• It is changed inside the function


echo $x; 19

• and those changes take effect outside echo subtractone ($x); 18


also
echo $x; 18

You might also like