The Arcive of Official vBulletin Modifications Site.It is not a VB3 engine, just a parsed copy! |
|
#1
|
||||
|
||||
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:
...and I made it this: PHP Code:
However, that doesn't seem to work. Can someone help me to understand what I did wrong? Thanks! |
#2
|
||||
|
||||
r u sure your returning something?
|
#3
|
||||
|
||||
Quote:
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:
|
#4
|
|||
|
|||
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; |
#5
|
||||
|
||||
Quote:
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? |
#6
|
||||
|
||||
Quote:
then you'll have PHP Code:
PHP Code:
If you want to return multiple values, return an array. |
#7
|
||||
|
||||
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.
|
#8
|
||||
|
||||
Quote:
|
#9
|
||||
|
||||
Quote:
|
#10
|
||||
|
||||
Quote:
The data is saved in the variable and this is usable like any other variable. |
Thread Tools | |
Display Modes | |
|
|
X vBulletin 3.8.12 by vBS Debug Information | |
---|---|
|
|
More Information | |
Template Usage:
Phrase Groups Available:
|
Included Files:
Hooks Called:
|