Go Back   vb.org Archive > vBulletin 3 Discussion > vB3 Programming Discussions
FAQ Community Calendar Today's Posts Search

Reply
 
Thread Tools Display Modes
  #1  
Old 10-25-2004, 10:58 PM
Ocean's Avatar
Ocean Ocean is offline
 
Join Date: Mar 2004
Posts: 208
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default 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:


PHP Code:
 
if ($_REQUEST['do'] == 'custom_action')
{
 
// Common code was here
 


...and I made it this:


PHP Code:
 
function custom_function()
{
 
// Common Code is now here
 
}
 
 
 
if (
$_REQUEST['do'] == 'custom_action')
{
 
     
custom_function();
 



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

Thanks!
Reply With Quote
  #2  
Old 10-26-2004, 12:35 AM
AN-net's Avatar
AN-net AN-net is offline
 
Join Date: Dec 2003
Location: AnimationTalk.com
Posts: 2,367
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

r u sure your returning something?
Reply With Quote
  #3  
Old 10-26-2004, 12:47 AM
Ocean's Avatar
Ocean Ocean is offline
 
Join Date: Mar 2004
Posts: 208
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
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:


PHP Code:
 
 
if ($testvariable == true)
{
 
// Common Section of Code
 
}
 
if (
$_REQUEST['do'] == 'custom_action')
{
 
 
// Unique Code goes here
 
 
// Duplicate of Common Section of Code
 
 
// More Unique Code goes here
 

Reply With Quote
  #4  
Old 10-26-2004, 01:41 AM
CarCdr CarCdr is offline
 
Join Date: Apr 2004
Posts: 242
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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 10-26-2004, 03:14 AM
Ocean's Avatar
Ocean Ocean is offline
 
Join Date: Mar 2004
Posts: 208
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
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?
Reply With Quote
  #6  
Old 10-26-2004, 04:38 AM
Colin F's Avatar
Colin F Colin F is offline
 
Join Date: Jul 2004
Location: Switzerland
Posts: 1,551
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
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

PHP Code:
function foo()
{
global 
$DB_site$bbuserinfo;
$fooreturn $bbuserinfo['usergroupid'];

return 
$fooreturn;

You'd then use it like this:
PHP Code:
if ($_REQUEST['do'] == "foo-it")
{

$foooutput foo();
echo 
$foooutput;


If you want to return multiple values, return an array.
Reply With Quote
  #7  
Old 10-26-2004, 07:54 AM
Natch's Avatar
Natch Natch is offline
 
Join Date: Nov 2002
Location: Australia
Posts: 851
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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.
Reply With Quote
  #8  
Old 10-26-2004, 11:05 AM
Ocean's Avatar
Ocean Ocean is offline
 
Join Date: Mar 2004
Posts: 208
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
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!
Reply With Quote
  #9  
Old 10-26-2004, 11:07 AM
Ocean's Avatar
Ocean Ocean is offline
 
Join Date: Mar 2004
Posts: 208
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
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?
Reply With Quote
  #10  
Old 10-26-2004, 11:16 AM
Colin F's Avatar
Colin F Colin F is offline
 
Join Date: Jul 2004
Location: Switzerland
Posts: 1,551
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
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
Reply


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

Forum Jump


All times are GMT. The time now is 08:59 AM.


Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2024, vBulletin Solutions Inc.
X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.04175 seconds
  • Memory Usage 2,274KB
  • Queries Executed 11 (?)
More Information
Template Usage:
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (1)ad_showthread_beforeqr
  • (1)ad_showthread_firstpost
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (5)bbcode_php
  • (6)bbcode_quote
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)navbar
  • (3)navbar_link
  • (120)option
  • (1)pagenav
  • (1)pagenav_curpage
  • (1)pagenav_pagelink
  • (10)post_thanks_box
  • (10)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (10)post_thanks_postbit_info
  • (10)postbit
  • (10)postbit_onlinestatus
  • (10)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available:
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files:
  • ./showthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_post_thanks.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • forumjump
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • post_thanks_function_post_thanks_off_start
  • post_thanks_function_post_thanks_off_end
  • post_thanks_function_fetch_thanks_start
  • post_thanks_function_fetch_thanks_end
  • post_thanks_function_thanked_already_start
  • post_thanks_function_thanked_already_end
  • fetch_musername
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • post_thanks_function_can_thank_this_post_start
  • pagenav_page
  • pagenav_complete
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • showthread_complete