Variable access

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
antubis
Forum Newbie
Posts: 16
Joined: Sun Apr 30, 2006 1:48 pm
Location: Pavlikeni, Bulgaria
Contact:

Variable access

Post 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
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: Variable access

Post 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 :)
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post 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
Post Reply