Dynamic Optional Parameter :)

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
User avatar
quocbao
Forum Commoner
Posts: 59
Joined: Sat Feb 04, 2006 2:03 am
Location: HCM,Vietnam
Contact:

Dynamic Optional Parameter :)

Post by quocbao »

This looks funny :D

test.php

Code: Select all

<?

function f($f = __FILE__ , $l = __LINE__)
{
	echo $f . ":" . $l . "<BR>";
}

?>
test2.php

Code: Select all

<?

include("test.php");

f();
f(__FILE__,__LINE__);

?>
this may output something like

Code: Select all

xxxxxxxxx\test.php:3
xxxxxxxxx\test2.php:6
Then PHP "prepares" our parameter before we call it or after we call it ( funny )
Last edited by quocbao on Mon Feb 20, 2006 3:56 am, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

what?
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Yes... those are constants, they are not dynamic, just the same as you can't do "5 = $foo;"

I'd love to know how you determine the line number of function was called from though... it would be massively useful in debugging.

backtrace() can help by the way if that's where you're heading ;)
User avatar
quocbao
Forum Commoner
Posts: 59
Joined: Sat Feb 04, 2006 2:03 am
Location: HCM,Vietnam
Contact:

Post by quocbao »

feyd wrote:what?
Well , this is a small script , i just want to get line number and file which this function is called from .
Of couse , using Debug backtrace will make life easier :P

I tried this

Code: Select all

<?

echo __LINE__; //3
echo __LINE__; //4


?>
so i think when we call this function

Code: Select all

function f($l = __LINE__,$f = __FILE__)
{
//want to do something here 
}
$l and $f will become the line and filename where it is called , but i was wrong :oops: (as you can see)
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

quocbao wrote:
feyd wrote:what?
Well , this is a small script , i just want to get line number and file which this function is called from .
Of couse , using Debug backtrace will make life easier :P

I tried this

Code: Select all

<?

echo __LINE__; //3
echo __LINE__; //4


?>
so i think when we call this function

Code: Select all

function f($l = __LINE__,$f = __FILE__)
{
//want to do something here 
}
$l and $f will become the line and filename where it is called , but i was wrong :oops: (as you can see)
Yep, because where __LINE__ and __FILE__ are parsed they will just the the same as the info for the part of the file you put them at ;) They don't change when they get used, they are fixed values :)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

debug_backtrace() can be used for this.
Post Reply