PHP: $_GET returns undefined index -


it has been few months since i've started learn php, i've got far, , whilst ran dozens of problems, have fortunately been able fix them, today's problem, not duplicate question, different, i've read stack overflow questions matter, of no help.

i getting following error:

notice: undefined index: city in /applications/xampp/xamppfiles/htdocs/index.php on line 99

this code:

<?php      $weather = "";     $error = "";      if ($_get['city']) {       $urlcontents = file_get_contents("http://api.openweathermap.org/data/2.5/weather?q=".urlencode($_get['city']).",uk&appid=hidden");          $weatherarray = json_decode($urlcontents, true);          if ($weatherarray['cod'] == 200) {              $weather = "the weather in ".$_get['city']." '".$weatherarray['weather'][0]['description']."'. ";              $tempincelcius = intval($weatherarray['main']['temp'] - 273);              $weather .= " temperature ".$tempincelcius."&deg;c , wind speed ".$weatherarray['wind']['speed']."m/s.";          } else {              $error = "could not find city - please try again.";          }      } ?> 

i using xampp , php.ini there too.

from code syntax think problem in how getting city. error indicates cannot find index of city in $_get; has not been set. add isset check before running code, prevent errors:

if ( isset($_get['city']) ) {     $urlcontents = file_get_contents("http://api.openweathermap.org/data/2.5/weather?q=".urlencode($_get['city']).",uk&appid=hidden");     //... } 

this prevent code run , avoid errors; check url make sure contains: https://example.com?city=somecity. should fix error.


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -