Passing parameters to require()'d functions

C++, C#, Java, PHP, ect...
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: Passing parameters to require()'d functions

Post by Jackolantern »

hallsofvallhalla wrote:ah i see what your saying..i had a real dumb moment :)

Noc might better answer this due to I dont use functions in PHP very often and when I do passing them works for me but I also haven't gotten into the require file that much. I use Javascript for my functions. But off the top of my head I can think of a alternative.

Make a variable

lets say $dthis

then in your function
display_this()
{
echo $dthis;
}

now before you pass any variable to it simply define $dthis
example:

$a = 10;
$b = 10;

$dthis = $a;
display_this();
$dthis = $b;
display_this();

that will print out
10
20

hope that helps and you have raised a question I must research for I am curious now too.


edit: or actually it will print out 1020 becasue I didnt break the line :)
While it would work, it is an inelegant method because the inner-workings of the function must be dug into, and the calling file must be modified to work with the function in the future. The best method is to be able to call the function using any variable name the author wants to send as a parameter. That way, you can just drop the function into any script and call it without having to re-write any variables or function inner-workings.

Or at least, that is the best method to me, being a child of OOP. 8-)
The indelible lord of tl;dr
User avatar
hallsofvallhalla
Site Admin
Posts: 12023
Joined: Wed Apr 22, 2009 11:29 pm

Re: Passing parameters to require()'d functions

Post by hallsofvallhalla »

that is definitely the best method, but I would just giving you a quick fix until you solve the main problem. I havent had time to dig further into your error. Sorry.
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: Passing parameters to require()'d functions

Post by Jackolantern »

hallsofvallhalla wrote:that is definitely the best method, but I would just giving you a quick fix until you solve the main problem. I havent had time to dig further into your error. Sorry.
No problem at all, and thanks for the help :D
The indelible lord of tl;dr
Post Reply

Return to “Coding”