0

I have this error in my PHP and I can't seem to understand why:

Notice: Undefined variable: site in C:\xampp\htdocs\ws5\process.php on line 12

I seem to get it for every variable im echoing in my switch statement

my code is:

<?php
$horfield = file_get_contents("http://www.martynhicks.uk/weather/clientraw.txt");
$thornbury = file_get_contents("http://www.thornburyweather.co.uk/weatherstation/clientraw.txt");
$gloucestershire = file_get_contents("https://www.glosweather.com/clientraw.txt");
$newquay = file_get_contents("http://www.newquayweather.com/clientraw.txt");

if (isset($_GET['site'])){
  $site = $_GET['site'];
}
switch ($site) {
  case 'Horfield':
echo $horfield;
break;
  case 'Thornbury':
echo $thornbury;
break;
  case 'Glouc':
echo $gloucestershire;
break;
  case 'Newquay':
echo $newquay;
break;
}
?>

(line 12 is the first echo statement 'echo $horfield', same occurs for the other 3.]

thanks

Luca
  • 25
  • 3
  • 1
    Your variable `$site` is only defined if it hits the `if (isset($_GET['site']))`. If it doesn't hit that block, it doesn't get defined. Define the variable before the if block to set it to a default value. – aynber Nov 23 '20 at 19:12
  • Thank you that solved it, very helpful! – Luca Nov 23 '20 at 20:32

0 Answers0