Page 1 of 1

Variable access

Posted: Tue May 23, 2006 3:42 am
by antubis
Hello,

I have this situation:

Code: Select all

function( $param1, $param2 );

$param2 = 'abcd';
$param2 is assigned later ( it can be assign before ) but function should use it. Is there any way PHP to check script till the end and if the varible exists to use it.

Regards,
Marush Denchev

Re: Variable access

Posted: Tue May 23, 2006 4:23 am
by Chris Corbyn
antubis wrote:Hello,

I have this situation:

Code: Select all

function( $param1, $param2 );

$param2 = 'abcd';
$param2 is assigned later ( it can be assign before ) but function should use it. Is there any way PHP to check script till the end and if the varible exists to use it.

Regards,
Marush Denchev
Not really no, if you're calling a function at a specific point in the flow of logic then it will only use the variables available at that time. Re-think your logic, or re-call the function if the value changes. This is where careful planning pays off :)

Posted: Tue May 23, 2006 4:30 am
by CoderGoblin
I should be noted however that you can have param2 as a default and make it optional

Code: Select all

function( $param1, $param2='abcd')
{
...
}
All optional parameters must be declared after required parameters

Code: Select all

function( $param1,$param2='xqz',$param3, $param4='abcd')
{
...
}
is not correct it must be

Code: Select all

function( $param1,$param3,$param2='xyz', $param4='abcd')
{
...
}
Regards