Register Members List Search Today's Posts Mark Forums Read

Reply
 
Thread Tools
  #1  
Old 26 Oct 2004, 00:58
Ocean's Avatar
Ocean Ocean is offline
 
Join Date: Mar 2004
Help with programming Functions in PHP

I have a section of code that needs to be present in multiple places in one of vB's PHP files. Rather than duplicating the code, I thought that I would just make that common section into a function - and just call that function from the various places within the PHP file.

However, I don't seem to have done it properly. To test the process with just one of the locations, this is what I did:


I effectively started out with this:



Block Disabled:      (Update License Status)  
Suspended or Unlicensed Members Cannot View Code.


...and I made it this:



Block Disabled:      (Update License Status)  
Suspended or Unlicensed Members Cannot View Code.



However, that doesn't seem to work. Can someone help me to understand what I did wrong?

Thanks!
__________________
echo $ocean_signature;

http://images.vbulletin.com/images_vb3/smilies/wink.gif
Reply With Quote
  #2  
Old 26 Oct 2004, 02:35
AN-net's Avatar
AN-net AN-net is offline
 
Join Date: Dec 2003
r u sure your returning something?
Reply With Quote
  #3  
Old 26 Oct 2004, 02:47
Ocean's Avatar
Ocean Ocean is offline
 
Join Date: Mar 2004
Originally Posted by AN-net

r u sure your returning something?
Ah, perhaps that's the problem. I wasn't looking to return anything - I was simply looking to execute code.

Perhaps functions are not what I need then...


Is there another way I can re-use the same section of code without needing to have two sets of it?

In other words, I'm trying to avoid this:



Block Disabled:      (Update License Status)  
Suspended or Unlicensed Members Cannot View Code.

__________________
echo $ocean_signature;

http://images.vbulletin.com/images_vb3/smilies/wink.gif
Reply With Quote
  #4  
Old 26 Oct 2004, 03:41
CarCdr CarCdr is offline
 
Join Date: Apr 2004
It's probably a scoping problem. Scoping in PHP is, well, lacking. In your function, if you want to reference a variable outside the function, such as $DB_site, or $bbuserinfo, or whatever, you have to declare it as global. You would do so using something like the following inside your function:

global $DB_site, $bbuserinfo;
Reply With Quote
  #5  
Old 26 Oct 2004, 05:14
Ocean's Avatar
Ocean Ocean is offline
 
Join Date: Mar 2004
Originally Posted by CarCdr

It's probably a scoping problem. Scoping in PHP is, well, lacking. In your function, if you want to reference a variable outside the function, such as $DB_site, or $bbuserinfo, or whatever, you have to declare it as global. You would do so using something like the following inside your function:

global $DB_site, $bbuserinfo;
Does that need to work in both directions? In other words, do I have to do that for variables created inside the function if I need to reference them outside of the function?

Also, that process is not insignificant in size for a function that references/uses many variables. Is there any way I can easily state that I want ALL variables within the function to be Global?
__________________
echo $ocean_signature;

http://images.vbulletin.com/images_vb3/smilies/wink.gif
Reply With Quote
  #6  
Old 26 Oct 2004, 06:38
Colin F's Avatar
Colin F Colin F is offline
 
Join Date: Jul 2004
Location: Switzerland
Originally Posted by Ocean
Does that need to work in both directions? In other words, do I have to do that for variables created inside the function if I need to reference them outside of the function?

Also, that process is not insignificant in size for a function that references/uses many variables. Is there any way I can easily state that I want ALL variables within the function to be Global?
If you want values calculated in the function to be available after, you'd probably want to do this by returning them.

then you'll have


Block Disabled:      (Update License Status)  
Suspended or Unlicensed Members Cannot View Code.

You'd then use it like this:

Block Disabled:      (Update License Status)  
Suspended or Unlicensed Members Cannot View Code.


If you want to return multiple values, return an array.
__________________
Reply With Quote
  #7  
Old 26 Oct 2004, 09:54
Natch's Avatar
Natch Natch is offline
 
Join Date: Nov 2002
You can use global to globalise variables from inside and outside: if you global $foo; inside a function then define an array $foo inside the function, you can then reference the array $foo[bar] etc outside the function, but only after the function has been called.
__________________
Natcher00
... is the Admin @ MobileForces.org - Official Community for a cult Vehicle based FPS - the only place for support!
... also he's Penfold to Dark_Wizard's Danger Mouse in development of vBWar
http://www.mobileforces.org/sig.jpg
{DaniWeb: Learn HOW-TO mod_rewrite your vB board}
Reply With Quote
  #8  
Old 26 Oct 2004, 13:05
Ocean's Avatar
Ocean Ocean is offline
 
Join Date: Mar 2004
Originally Posted by Natch

You can use global to globalise variables from inside and outside: if you global $foo; inside a function then define an array $foo inside the function, you can then reference the array $foo[bar] etc outside the function, but only after the function has been called.
Makes sense, and works perfectly. Thanks!
__________________
echo $ocean_signature;

http://images.vbulletin.com/images_vb3/smilies/wink.gif
Reply With Quote
  #9  
Old 26 Oct 2004, 13:07
Ocean's Avatar
Ocean Ocean is offline
 
Join Date: Mar 2004
Originally Posted by Colin F

If you want values calculated in the function to be available after, you'd probably want to do this by returning them.
I would think that echoing them only makes them useable only at that moment. If I'm wrong, than is there a reason you would recommend that method over globalising them from inside the function? Or would they be interchangeable in results?
__________________
echo $ocean_signature;

http://images.vbulletin.com/images_vb3/smilies/wink.gif
Reply With Quote
  #10  
Old 26 Oct 2004, 13:16
Colin F's Avatar
Colin F Colin F is offline
 
Join Date: Jul 2004
Location: Switzerland
Originally Posted by Ocean
I would think that echoing them only makes them useable only at that moment. If I'm wrong, than is there a reason you would recommend that method over globalising them from inside the function? Or would they be interchangeable in results?
I'm not sure what you mean by "only at that moment".
The data is saved in the variable and this is usable like any other variable.
__________________
Reply With Quote
  #11  
Old 26 Oct 2004, 13:35
Ocean's Avatar
Ocean Ocean is offline
 
Join Date: Mar 2004
Originally Posted by Colin F

I'm not sure what you mean by "only at that moment".
The data is saved in the variable and this is usable like any other variable.
I thought "echo" was an immediate output function - so that if you ran the following...


Block Disabled:      (Update License Status)  
Suspended or Unlicensed Members Cannot View Code.


...you would have "10" displayed on screen.

Was I mistaken?
__________________
echo $ocean_signature;

http://images.vbulletin.com/images_vb3/smilies/wink.gif
Reply With Quote
  #12  
Old 26 Oct 2004, 14:28
Colin F's Avatar
Colin F Colin F is offline
 
Join Date: Jul 2004
Location: Switzerland
Originally Posted by Ocean
I thought "echo" was an immediate output function - so that if you ran the following...


Block Disabled:      (Update License Status)  
Suspended or Unlicensed Members Cannot View Code.


...you would have "10" displayed on screen.

Was I mistaken?
Oh of course

I just wrote the echo line to show that the variable can be used normally. You don't have to echo it, you can use it like any other variable.
__________________
Reply With Quote
  #13  
Old 26 Oct 2004, 18:51
Ocean's Avatar
Ocean Ocean is offline
 
Join Date: Mar 2004
Originally Posted by Colin F

Oh of course

I just wrote the echo line to show that the variable can be used normally. You don't have to echo it, you can use it like any other variable.
My mistake.


Well, that being the case - I'm still curious - is there any difference in function or performance between returning the values or globalising the variables? Or are there no significant differences or merits to using one as opposed to the other?
__________________
echo $ocean_signature;

http://images.vbulletin.com/images_vb3/smilies/wink.gif
Reply With Quote
  #14  
Old 26 Oct 2004, 23:56
Natch's Avatar
Natch Natch is offline
 
Join Date: Nov 2002
Globalising a variable is useful IMO when you have a lot of data you wanna pass back in many different variables, whereas a small function for a sepcific purpose might just return one value or array, in which case returning the variable and keeping it out of the $GLOBALS array may be more suitable.

Two examples:

- passing a number to a function to return a ordinalised string (1, 2, 3 => 1st, 2nd, 3rd) would be better simply to have
Block Disabled:      (Update License Status)  
Suspended or Unlicensed Members Cannot View Code.

and
Block Disabled:      (Update License Status)  
Suspended or Unlicensed Members Cannot View Code.

NOTE: I know this could be switched, but this is just an example)

- If you are trying to test for or generate a data caches, you might be better off globalising them at the start of the function, then you wont have to create a multi-dimensional array of the values you want to return, then
Block Disabled:      (Update License Status)  
Suspended or Unlicensed Members Cannot View Code.

afterwards...

I hope that makes sense?
__________________
Natcher00
... is the Admin @ MobileForces.org - Official Community for a cult Vehicle based FPS - the only place for support!
... also he's Penfold to Dark_Wizard's Danger Mouse in development of vBWar
http://www.mobileforces.org/sig.jpg
{DaniWeb: Learn HOW-TO mod_rewrite your vB board}
Reply With Quote
  #15  
Old 27 Oct 2004, 04:01
Ocean's Avatar
Ocean Ocean is offline
 
Join Date: Mar 2004
Originally Posted by Natch

Globalising a variable is useful IMO when you have a lot of data you wanna pass back in many different variables, whereas a small function for a sepcific purpose might just return one value or array, in which case returning the variable and keeping it out of the $GLOBALS array may be more suitable.
I think I understand.
__________________
echo $ocean_signature;

http://images.vbulletin.com/images_vb3/smilies/wink.gif
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


New To Site? Need Help?

All times are GMT. The time now is 04:23.

Layout Options | Width: Wide Color: