Forum Moderators: coopster

Message Too Old, No Replies

Creating New Function With Variables But Without Defining Them

Creating New Function With Variables But Without Defining Them

         

ideffect

2:59 am on Jan 14, 2009 (gmt 0)

10+ Year Member



Hello,

I have to create a few new functions to work with a complex form and I was wondering if it is possible to use variables in the function without defining them?

I hope that makes sense.
Thanks!

penders

10:21 am on Jan 14, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



In PHP, when you first assign something to a variable it is then defined. You don't have to explicitly 'define' it. However, it is always good practise to initialise/define your variables early on in your code.

Is that what you mean? Or are you referring to GET/POST values coming from your form?

ideffect

8:57 pm on Jan 14, 2009 (gmt 0)

10+ Year Member



So here is an example of what I would like to do but I do not think it is possible.

So, the code below will obviously display "TEST 123"


$var = "TEST 123";

function test($var) {
echo "$var";
}
test($var);

Now, this is what I would like to do and still display "TEST 123"


$var = "TEST 123";

function test() {
echo "$var";
}
test();

Thanks!

dreamcatcher

9:29 pm on Jan 14, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



function test() {
global $var;
echo "$var";
}

or

function test($var) {
echo "$var";
}

dc

ideffect

3:10 am on Jan 15, 2009 (gmt 0)

10+ Year Member



Thanks DC
Using global $var; should make it a little easier.