0

I'm trying to make a couple functions to get the amount of hours of light and dark based on sunrise and sunset times. Before moving my code into functions everything was working fine, but now it is unable to define my variables despite the fact that those variables are printing perfectly outside of the function. I've looked through a few answers on here, but every one seems to suggest using either global or by passing the variables as arguments. Using global causes it to not work at all, and passing the variables as arguments didn't change anything. I have two files, one with my functions, and one to print them.

The error is this:

Notice: Undefined variable: sunset in C:\xampp\htdocs\test.php on line 13

Notice: Undefined variable: sunrise in C:\xampp\htdocs\test.php on line 13

My functions are as follows:

<html>
<body>

<?php

$lat = 38.7;
$long = -75;

$sunrise = (date_sunrise(time(),SUNFUNCS_RET_STRING,38.7,-75,75,-5)); //get local time, offset -5 hours from gmt
$sunset = (date_sunset(time(),SUNFUNCS_RET_STRING,38.7,-75,75,-5));   //get local time, offset -5 hours from gmt

function light(){ //passing the variables gave me the same result
    (strtotime($sunset) - strtotime($sunrise))/3600;   //$sunset and $sunrise are undefined
}

function dark(){
    24 - light(); //same issues as light()
}

?>

</body>
</html>

And this is what I'm using to print everything:

<?php
                    require "test.php"; //name of function page

                    echo "Date: " . date("D M j Y");
                    echo "<br>Latitude: " . $lat;
                    echo "<br>Longitude: " . $long . "<br>";

//these variables are the ones that are undefined in my function, but they're printing just fine here
                    echo "<br>Sunrise Time: " . $sunrise; 
                    echo "<br>Sunset Time: " . $sunset . "<br>";

                    echo "<br>Hours of Light: " . (round(light(),2)); //round to two decimals
                    echo "<br>Hours of Dark: " . (round(dark(),2));   //round to two decimals
                ?>

I'm kind of at a loss here, I was thinking that perhaps it was because I was using pre-built functions in my own, but I had thought for sure that it was possible to do this. I've been trying to troubleshoot this for the past day or so with no progress, so any help would be hugely appreciated. Thank you!

Kaijudile
  • 59
  • 5

2 Answers2

1

You need to read more on variable scope. You cannot access the outside variables from inside the function.

Instead modify your function like this:

<?php

$lat = 38.7;
$long = -75;

$sunrise = (date_sunrise(time(),SUNFUNCS_RET_STRING,38.7,-75,75,-5)); //get local time, offset -5 hours from gmt
$sunset = (date_sunset(time(),SUNFUNCS_RET_STRING,38.7,-75,75,-5));   //get local time, offset -5 hours from gmt

function light($sunset, $sunrise){ //passing the variables gave me the same result
    (strtotime($sunset) - strtotime($sunrise))/3600;
}

$light = light($sunset, $sunrise)

function dark($light){
    24 - $light;
 }

?>
Gabriel
  • 970
  • 7
  • 20
0

I was actually able to fix this by reading through here https://www.php.net/manual/en/language.variables.scope.php

turns out that what I needed to do was define the variables as global in the function itself, not outside of it!

Kaijudile
  • 59
  • 5