___________________________________________________________
You definitely never want to have a dollar sign in front of a function
____________
_______
<?php
function editGlobal($globalVariable)//Pass it in
{
$globalVariable = 200;//Try to change the value
}
$globalVariable = 120;//
echo $globalVariable, "<br><br>";
editGlobal($globalVariable);//Fails
echo $globalVariable, "<br><br>";//Prints the same thing
?>
You need to reference a variable if it is out of scope
<?php
function editGlobal(&$globalVariable)//Reference it
{
$globalVariable = 200;
}
$globalVariable = 120;
echo $globalVariable, "<br><br>";
editGlobal($globalVariable);//Success
echo $globalVariable, "<br><br>";
?>
RECURSION
Calling a function from inside of itself
1.<?php
2. function factorial($number)
3. {//What are passed variables between friends, huh(?)
4. if($number == 1)
5. {
6. return 1;
7. }
8. else
9. {
10. return $number * factorial($number - 1);
11. }
12. }
13. echo factorial(3);
?>
This is similar to a looping. Line 10 calls the function from within itself , subtracting 1 each time then multiplying it by the value of $number until the condition on line 4 is met.
No comments:
Post a Comment