View previous topic :: View next topic |
Author |
Message |
kev1952 -
Joined: 08 Sep 2005 Posts: 105 Location: Townsville Australia
|
Posted: Wed Feb 08, 2006 11:30 am Post subject: Functions |
|
|
I am having a great deal of trouble getting my head around "functions" in PHP. It's not that I don't understand the use of a function but the logic is puzzling me. I found a simple function (for test purposes) on the net and when I try it I get this error:
Fatal error: Cannot re-assign $this in E:\Website\htdocs\func.php on line 4
The code is:
Code: | <?php
function test_scope()
{
$this = 'that';
return $this;
}
// Call the function
$this = test_scope();
// Echo $this
echo $this;
?> |
To me it looks okay but when I try it it returns the above error. I feel like I'm falling down a very big hole with functions and have yet to see the way out. I'm sure it's very simple once you know how to do it but I just can't seem to find the correct way.
Perhaps someone here can either explain it to me or point me to a resource that will explain it. I'm tearing my hair out with it! :( _________________ Cheers.... Kev
Kev's Place - http://www.kevsplace.com
Powered by Abyss X1. |
|
Back to top |
|
 |
MonkeyNation -
Joined: 05 Feb 2005 Posts: 921 Location: Cardiff
|
Posted: Wed Feb 08, 2006 11:34 am Post subject: |
|
|
Well, without using global, when you make a function, all variables from outside it (With the exception of superglobals($_GET, etc)) are erased. However, when a function is executed, the variabled created inside it are kept. (Might be a way around it, never had to find out.)
If you do:
Code: | $that = test_scope(); |
- I believe it will work. (Or just echo test_scope();) _________________
 |
|
Back to top |
 |
 |
kev1952 -
Joined: 08 Sep 2005 Posts: 105 Location: Townsville Australia
|
Posted: Wed Feb 08, 2006 11:45 am Post subject: |
|
|
Thanks, MN, but neither way worked. Still go the same error. I actually had tried that before but re-ran it only to get the same result. _________________ Cheers.... Kev
Kev's Place - http://www.kevsplace.com
Powered by Abyss X1. |
|
Back to top |
|
 |
MonkeyNation -
Joined: 05 Feb 2005 Posts: 921 Location: Cardiff
|
Posted: Wed Feb 08, 2006 11:58 am Post subject: |
|
|
Don't use $this in that manner, basically; it's a documented bug in PHP.
Code: | function is_even_number($number) {
if ($number < 2)
return false;
if (strstr((string)($number/2), ".")) //The (string) just makes it a string, so we can check for a decimal point.
return false;
return true;
}
//Usage:
if (is_even_number(7))
echo "Nice going!";
else
echo "Unlucky."; |
_________________
 |
|
Back to top |
 |
 |
kev1952 -
Joined: 08 Sep 2005 Posts: 105 Location: Townsville Australia
|
Posted: Wed Feb 08, 2006 12:43 pm Post subject: |
|
|
Now it's starting to make some sense. I see how you passed the variables between the usage and the function - and the original code worked when I changed the name of the variable.
I can expand on that now. Thanks! _________________ Cheers.... Kev
Kev's Place - http://www.kevsplace.com
Powered by Abyss X1. |
|
Back to top |
|
 |
|