SilentCry
SilentCry

Reputation: 2092

PHP What is wrong with recursive function?

i did function for showing today date or next workday date, if is weekend. Function works great, but with return is something wrong.

$today = todayDate('2014-10-18'); // Saturday

    function todayDate($date) {
        if(date('N', strtotime($date)) >= 6) {
            echo 'If - ' . $date . '<br/>';
            $date = date('Y-m-d', strtotime('+1 day', strtotime($date)));
            todayDate($date);
        } else {
            echo 'Else - ' . $date . '<br/>';               
        }
        return $date;
    }

    echo '<br/><br/>Today: ' . $today . '<br/><br/>';

This function echoes following:

If - 2014-10-18
If - 2014-10-19
Else - 2014-10-20

But echo of $today (last row in code) is

Today: 2014-10-19

So, what is wrong? Last $date in function is "2014-10-20" and this value is returning to $today, but $today shows different value. Any idea?

Upvotes: 0

Views: 85

Answers (1)

andy
andy

Reputation: 2002

As kojiro pointed out in the comment, you do not assign the return value of the inner call to todayDate(). To change this, replace this line

todayDate($date);

with

$date = todayDate($date);

Upvotes: 1

Related Questions