Link to home
Start Free TrialLog in
Avatar of Jorge Batres
Jorge BatresFlag for United States of America

asked on

PHP 7 Warning

Hi everybody, I need help with this piece of code that is generation a warning in PHP 7 but is actually breaking a price calculation as well:

This is the code I need to modify:
$season = end($season_dates);
                for ($night = $nights; $night > $BaseNights; --$night) {
                  if ($season == 'High' and $HighAddPrice) {
                    $price += $HighAddPrice;
                  } 
				  elseif ($season == 'Holiday' and $HolidayAddPrice) {
                    $price += $HolidayAddPrice;
                  }
				  
				  else {
                    $price += $AddPrice;
                  }
                  $season = prev($season_dates);

Open in new window


And these are the errors I'm getting:

mod_fcgid: stderr: PHP Warning: end() expects parameter 1 to be array, null given in line 1

mod_fcgid: stderr: PHP Warning: prev() expects parameter 1 to be array, null given in line 13

Could somebody please tell me what I need to change or modify and how?

Thanks,
Jorge
SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Jorge Batres

ASKER

Hi Julian, I tried that but it didn't make a difference.
There is also this line a few lines above the code

$season_dates = array();

Open in new window

I would need to see the rest of the code, but my guess is that the declaration is outside the scope of the function from which you gave us the code snippet.

Meaning that in the context of the function, $season_dates is not defined.

HTH,
Dan
Try adding error_reporting(E_ALL) to the top of your PHP scripts.  This will help you know if a variable is undefined.

One possible way this could happen might be if data fetched from a database was not returned correctly.  Higher error reporting levels will not always catch that, but can help.
Hi Julian, I tried that but it didn't make a difference.
It was not meant to make a difference it was meant to dump the $season_dates value so we could see the type and contents.

What was the output from that line?
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
First of all, thank you everybody for your input. This price calculation calls for 2 separate text files for hotel prices. The first text file type, has a list of hotels where the price is the same all year round and it looks like this:

[The Hotel Name:Standard Room Double Beds]
BasePrice = 180
BaseNights = 3
AddPrice = 70
MaxNights = 5

The second text file includes the hotels that have different prices according to seasons and looks like this:

[The Hotel Name:Standard Room King Bed]
HolidaySeasonDates = 21 December / 31 December; 1 January / 5 January ; 19 March / 3 April
HolidaySeasonPrice = 600
HighSeasonDates = 6 January / 18 March ; 4 April / 21 August
HighSeasonPrice = 450
BasePrice = 300
BaseNights = 3
AddPrice = 120
HighAddPrice = 170
HolidayAddPrice = 225
MaxNights = 7

Before PHP 7, $nights <= $BaseNights used to calculate fine, (although it contains High and Holiday seasons that really should not apply here) but with PHP 7, that section doesn't work any more and it only shows price for 3 nights (BaseNights). So to make it work, I had to temporarily move the all the hotels to the second text file and just assign the same price for different seasons, and that works without an error.

So I think the problem is how the code below regarding $nights <= $BaseNights is the one that needs to be fixed.

if ($BaseNights==0) {
                $price = 0;
            }
            elseif ($nights <= $BaseNights) {
                if ($mode) {
                    $price = ($season_days['High']*$HighPrice + $season_days['Holiday']*$HolidayPrice + ($nights-$season_days['High']-$season_days['Holiday'])*$BasePrice)/$BaseNights;
                }
                else {
                    $price = $nights*$BasePrice/$BaseNights;
                }
            }
            elseif ($nights <= $MaxNights) {
                if ($mode) {
                    $price = min($season_days['Holiday'], $BaseNights) * $HolidayPrice;
                    $price += max(min($season_days['High'], $BaseNights - $season_days['Holiday']), 0) * $HighPrice;
                    $price += max(min($nights - $season_days['High'] - $season_days['Holiday'], $BaseNights - $season_days['High'] - $season_days['Holiday']), 0) * $BasePrice;
                    $price /= $BaseNights;
                }
                else {
                    $price = $BasePrice;
                }

Open in new window

... can we have the var_dump of $season_days because the code above does not show us where it is being set i.e. on the left of the equation.
Gentleman, I just moved all my hotels to the text file that has different seasons and everything works without generating any errors.

That is good enough for me at this point.

I appreciate all your input.
Thank you for your help, much appreciated