Kevin Cranford
asked on
PHP function looping when it should not be.
This isn't very critical since it is allowing me to still get the result I am looking for, but it still bothers me.
I have the following php function (the $runnumber and echo statements were added to follow the flow, they will be removed):
The problem is that the results of this are as follows:
As you can see in run number 3 it has used the correct day, which is not an excluded day, and returned 0 results. At that point, in line 26 of the code it should have failed the if statement and went straight to the return $dueDate on line 33. But instead, it runs again using the date it was prior to the increase, what it was in run number 2, and then runs it again and successfully returns the correct date.
While I am getting the correct result I am concerned that whatever is causing this could create an issue in the future. Any help would be appreciated. Also if you know of a more elegant solution to handle this it would be appreciated as well.
I have the following php function (the $runnumber and echo statements were added to follow the flow, they will be removed):
function verifydate($dueDate,$length,$runnumber) {
echo "Run number: " . $runnumber . "<br>";
echo "Current due date: " . $dueDate . "<br>";
$dayName = strftime("%A", strtotime("$dueDate"));
echo "Current day of week: " . $dayName . "<br>";
if ($dayName == "Saturday") {
$length = $length + 2;
$dueDate = date("Y-m-d", mktime(0, 0, 0, date("m"),date("d")+$length,date("Y")));
echo "Since the day is Saturday, adding 2 more days to the due date and checking again.<br><br>";
$runnumber = $runnumber + 1;
verifydate($dueDate,$length,$runnumber);
}
if ($dayName == "Sunday") {
$length = $length + 1;
$dueDate = date("Y-m-d", mktime(0, 0, 0, date("m"),date("d")+$length,date("Y")));
echo "Since the day is Saturday, adding 1 more day to the due date and checking again.<br><br>";
$runnumber = $runnumber + 1;
verifydate($dueDate,$length,$runnumber);
}
$conn2 // a connection to my database.
$sql = "SELECT dayID FROM excludedDays WHERE excludedDay = '$dueDate'";
$result = $conn2->query($sql);
echo "Since the day is not on a weekend, checking to see if it is an excluded day using the MySQL query:<br>";
echo $sql . "<br>";
echo "Which returned " . $result->num_rows . " result.<br>";
if ($result->num_rows > 0) {
$length = $length + 1;
$dueDate = date("Y-m-d", mktime(0, 0, 0, date("m"),date("d")+$length,date("Y")));
$runnumber = $runnumber + 1;
echo "Since the date was excluded the new due date is: " . $dueDate . "<br><br>";
verifydate($dueDate,$length,$runnumber);
}
return $dueDate;
}
$dateDue = verifydate("2017-09-16", 1, 1);
echo "<br>The final due date is: " . $dateDue . "<br>";
The problem is that the results of this are as follows:
Run number: 1
Current due date: 2017-09-16
Current day of week: Saturday
Since the day is Saturday, adding 2 more days to the due date and checking again.
Run number: 2
Current due date: 2017-09-18
Current day of week: Monday
Since the day is not on a weekend, checking to see if it is an excluded day using the MySQL query:
SELECT dayID FROM excludedDays WHERE excludedDay = '2017-09-18'
Which returned 1 result.
Since the date was excluded the new due date is: 2017-09-19
Run number: 3
Current due date: 2017-09-19
Current day of week: Tuesday
Since the day is not on a weekend, checking to see if it is an excluded day using the MySQL query:
SELECT dayID FROM excludedDays WHERE excludedDay = '2017-09-19'
Which returned 0 result.
Since the day is not on a weekend, checking to see if it is an excluded day using the MySQL query:
SELECT dayID FROM excludedDays WHERE excludedDay = '2017-09-18'
Which returned 1 result.
Since the date was excluded the new due date is: 2017-09-19
Run number: 3
Current due date: 2017-09-19
Current day of week: Tuesday
Since the day is not on a weekend, checking to see if it is an excluded day using the MySQL query:
SELECT dayID FROM excludedDays WHERE excludedDay = '2017-09-19'
Which returned 0 result.
The final due date is: 2017-09-19
As you can see in run number 3 it has used the correct day, which is not an excluded day, and returned 0 results. At that point, in line 26 of the code it should have failed the if statement and went straight to the return $dueDate on line 33. But instead, it runs again using the date it was prior to the increase, what it was in run number 2, and then runs it again and successfully returns the correct date.
While I am getting the correct result I am concerned that whatever is causing this could create an issue in the future. Any help would be appreciated. Also if you know of a more elegant solution to handle this it would be appreciated as well.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
The code I added just prior to the return statement is:
Open in new window
My result is:Open in new window
As you can see in line 20 in echoed the string correctly and should have then hit the return statement and left the function. Instead, it ran again, matched the correct date again, echoed the string again, then somehow skipped everything and took two days off of the date, echoed that string and then finally hit the return statement. Which in this case, it gave me the wrong date because it should never return a date that is a Saturday.