# ok, I'm stuck: recursive function restting vars and not returning when expected

#### Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

•  Subject
• Author
• Posted on
arrrrrg:
Condo for rent has 3 price tiers (for different times of the year):
value
regular

For every 7 nites they stay, they get 1 free, and that free one should be the
cheapest night (1 value nite, 6 premium nites, they should get the value nite
free)

main script:
\$_POST['vnites'] = 2;  \$_POST['rnites'] = 0;  \$_POST['pnites']=12;

\$totalnites = \$_POST['vnites'] + \$_POST['rnites'] + \$_POST['pnites'];
echo 'totalnites= '. \$totalnites.'<BR><BR>';
\$postNights = array('vnites' => \$_POST['vnites'] , 'rnites' => \$_POST['rnites']
, 'pnites' => \$_POST['pnites']);
if ((\$totalnites >  6)AND(\$totalnites < 14)){\$mult = 1;}
if ((\$totalnites > 13)AND(\$totalnites < 21)){\$mult = 2;}
if ((\$totalnites > 20)AND(\$totalnites < 28)){\$mult = 3;}
if ((\$totalnites > 27)AND(\$totalnites < 35)){\$mult = 4;}
if ((\$totalnites > 34)AND(\$totalnites < 42)){\$mult = 5;}

\$temptotalnites = \$totalnites;

if (\$totalnites > 6){ // call the func:
\$discountnites = subtractFreeDays(\$postNights ,\$temptotalnites, \$mult);

echo '<PRE>after func \$discountnites=';
print_r(\$discountnites);
echo '</PRE>';
}

func:
<?php
function subtractFreeDays(\$fpostNights, \$ftotalnites, \$fmult){
echo 'in func mult: '.\$fmult.'<BR>';
echo 'in func \$fpostNights[vnites]: '.\$fpostNights['vnites'].'<BR>';
echo 'in func \$discvnites='.\$discNights['vnites']."<BR><BR>";

if (\$fmult > 0){

if(\$fpostNights['vnites'] > 0){//echo'1';
\$discNights['vnites'] = \$discNights['vnites'] + 1;
echo '\$discvnites='.\$discNights['vnites']."<BR>";
\$fpostNights['vnites'] = \$fpostNights['vnites'] - 1;
echo '\$fpostNights[vnites]='.\$fpostNights['vnites']."<BR><BR>";
}
elseif(\$fpostNights['rnites'] > 0){
\$discNights['rnites'] = \$discNights['rnites'] + 1;
\$fpostNights['rnites'] = \$fpostNights['rnites'] - 1;
}
else{
\$discNights['pnites'] = \$discNights['pnites'] + 1;
\$fpostNights['pnites'] = \$fpostNights['pnites'] - 1;
}
//echo 'before minusing fmlut='.\$fmult."<BR>";
\$fmult = \$fmult - 1;
if (\$fmult >= 1){
echo 'before recurse \$fmult= '.\$fmult.'<HR>';
subtractFreeDays(\$fpostNights, \$ftotalnites, \$fmult);
}else{
echo '<PRE>returning:';
print_r(\$discNights);
echo '</PRE>';
return \$discNights;
}
}
//shouldnt get here
return array('shouldnt','get','here');
}
?>

-- This works on less than 14 days.
Over 14 days, it breaks:
1. \$discNights['vnites'] is being reset to nothing every recursion, what gives?
2. I get to
echo '<PRE>returning:';
print_r(\$discNights);
echo '</PRE>';
return \$discNights;
and it *does* print_r (wrong value, see #1), but it doesnt seem to return, and
that really chaps my hide.  It will keep going, eventually returning the
array('shouldnt','get','here').

## Re: ok, I'm stuck: recursive function restting vars and not returning when expected

If this is an application for real use, consider how likely it would
be that someone decides that there will be a fourth price tier, and

If nites is how you spell nights, it's fine with me. If another
English programmer will work on your code, (s)he will make mistakes
very soon. However, if you are using both "nights" and "nites", as you
do, you are asking for bugs. Even if you are the only one working on
the program.

Use math instead of this. It's simpler and it works on any number of
nights. Something like
\$mult = floor(\$totalnites / 7);

Anything with "temp" is typically a bad variable name.

Always call the function, no matter how many nights are booked. This
allows easier change if it is decided that every third night is free.

I have something here which does not use recursion. This is not a
recursive problem anyway:

\$_POST['vnites'] = 2;  \$_POST['rnites'] = 0;  \$_POST['pnites']=12;

\$types = array('vnites', 'rnites', 'pnites');

foreach (\$types as \$type) {
\$postNights[\$type] = \$_POST[\$type];
}

\$totalNights = 0;
foreach (\$postNights as \$type => \$nights) {
\$totalNights += \$nights;
}

\$freeNights = floor(\$totalNights/7);
for (\$i = 0; \$i < \$freeNights; \$i++) {
foreach (\$types as \$type) {
if (\$postNights[\$type] > 0) {
\$postNights[\$type] --;
\$postNights['freenites'] ++;
// do not consider other types, continue with for loop
continue 2;
}
}
}

print_r(\$postNights);

?>

## Re: ok, I'm stuck: recursive function restting vars and not returning when expected

Kimmo Laine wrote:

Unless next_page.php generates PHP, the script with this include will
only get HTML.

next_page.php

<?php
if (isset(\$_GET['foo'])) {
echo '<?php echo \$_GET[\'foo\']; ?>';
} else {
echo '<?php echo \'Not available\'; ?>';
}

--