[RESOLVED] function acting weird

liunx

Guest
given:


function smallBOX($color='gray',$head='',$body='')
{
echo $color;
echo $head;
echo $body;
}

echo smallBOX( '','head_1','body_1');

echo smallBOX('gray','head_2','body_2');


im getting:



head_1
body_1

gray
head_2
body_2


why isnt it accepting the default input of gray in the first call?function smallBOX($head = '', $body = '', $color = 'gray')
{
echo $color . ' ' . $head . ' ' . $body;
}

smallBOX('head_1','body_1'); // outputs "gray head_1 body_1"You can only omit optional parameters from right to left, such as:

echo smallBOX('gray','head_2','body_2');
echo smallBOX('gray','head_2');
echo smallBOX('gray');
echo smallBOX();

With echo smallBOX('','head_1','body_1'); you are actually passing a value for the first arg, even if it is just an empty string. You would have to add some additional logic to your function if you want to apply a default in that case:

function smallBOX($color='gray',$head='',$body='')
{
if(trim($color) === '')
{
$color = 'gray';
}
echo $color;
echo $head;
echo $body;
}excellent... so really you can only "omit" the last parameter due to the way the functions are "read"... learned something new

thanks dude
 
Back
Top