vb.org Archive

vb.org Archive (https://vborg.vbsupport.ru/index.php)
-   vB3 Programming Discussions (https://vborg.vbsupport.ru/forumdisplay.php?f=15)
-   -   Help with programming Functions in PHP (https://vborg.vbsupport.ru/showthread.php?t=70992)

Ocean 10-25-2004 10:58 PM

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! :)

AN-net 10-26-2004 12:35 AM

r u sure your returning something?

Ocean 10-26-2004 12:47 AM

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
 



CarCdr 10-26-2004 01:41 AM

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;

Ocean 10-26-2004 03:14 AM

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?

Colin F 10-26-2004 04:38 AM

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.

Natch 10-26-2004 07:54 AM

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.

Ocean 10-26-2004 11:05 AM

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!

Ocean 10-26-2004 11:07 AM

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?

Colin F 10-26-2004 11:16 AM

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.


All times are GMT. The time now is 08:32 PM.

Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2025, vBulletin Solutions Inc.

X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.01225 seconds
  • Memory Usage 1,756KB
  • Queries Executed 10 (?)
More Information
Template Usage:
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (5)bbcode_php_printable
  • (6)bbcode_quote_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)pagenav
  • (1)pagenav_curpage
  • (1)pagenav_pagelink
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (10)printthreadbit
  • (1)spacer_close
  • (1)spacer_open 

Phrase Groups Available:
  • global
  • postbit
  • showthread
Included Files:
  • ./printthread.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/class_bbcode_alt.php
  • ./includes/class_bbcode.php
  • ./includes/functions_bigthree.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
  • printthread_start
  • pagenav_page
  • pagenav_complete
  • bbcode_fetch_tags
  • bbcode_create
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • printthread_post
  • printthread_complete