Functions

 
Post new topic   Reply to topic    Aprelium Forum Index -> PHP
View previous topic :: View next topic  
Author Message
kev1952
-


Joined: 08 Sep 2005
Posts: 105
Location: Townsville Australia

PostPosted: Wed Feb 08, 2006 11:30 am    Post subject: Functions Reply with quote

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 View user's profile Send private message Send e-mail Visit poster's website
MonkeyNation
-


Joined: 05 Feb 2005
Posts: 921
Location: Cardiff

PostPosted: Wed Feb 08, 2006 11:34 am    Post subject: Reply with quote

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 View user's profile Send private message AIM Address Yahoo Messenger MSN Messenger ICQ Number
kev1952
-


Joined: 08 Sep 2005
Posts: 105
Location: Townsville Australia

PostPosted: Wed Feb 08, 2006 11:45 am    Post subject: Reply with quote

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 View user's profile Send private message Send e-mail Visit poster's website
MonkeyNation
-


Joined: 05 Feb 2005
Posts: 921
Location: Cardiff

PostPosted: Wed Feb 08, 2006 11:58 am    Post subject: Reply with quote

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 View user's profile Send private message AIM Address Yahoo Messenger MSN Messenger ICQ Number
kev1952
-


Joined: 08 Sep 2005
Posts: 105
Location: Townsville Australia

PostPosted: Wed Feb 08, 2006 12:43 pm    Post subject: Reply with quote

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 View user's profile Send private message Send e-mail Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    Aprelium Forum Index -> PHP All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB phpBB Group