tt - PHP Online

Form of PHP Sandbox

Enter Your PHP code here for testing/debugging in the Online PHP Sandbox. As in the usual PHP files, you can also add HTML, but do not forget to add the tag <?php in the places where the PHP script should be executed.



Your result can be seen below.

Result of php executing





Full code of tt.php

  1. <?php
  2. // Function to validate a location using the Google Maps Geocoding API
  3. function validateLocation($location) {
  4.     $apiKey = 'your_api_key'; // Replace with your own Google API key
  5.  
  6.     // Replace spaces with "+" in the location for URL compatibility
  7.     $location = str_replace(" ", "+", $location);
  8.  
  9.     $url = "https://maps.googleapis.com/maps/api/geocode/json?address=".$location."&key=".$apiKey;
  10.  
  11.     $response = file_get_contents($url);
  12.  
  13.     $data = json_decode($response);
  14.  
  15.     $status = $data->status;
  16.  
  17.     if ($status == "OK") {
  18.         return true; // Valid location
  19.     } else {
  20.         return false; // Invalid location
  21.     }
  22. }
  23.  
  24. // Usage example:
  25. $location = "1600 Amphitheatre Parkway, Mountain View, CA";
  26.  
  27. if (validateLocation($location)) {
  28.     echo "Valid location";
  29. } else {
  30.     echo "Invalid location";
  31. }
  32. ?>
  33.  
File Description
  • tt
  • PHP Code
  • 27 May-2023
  • 862 Bytes
You can Share it: