How can PHP functions be used as web services?

PHP functions can be used as web services by creating a PHP file that contains the function you want to expose as a web service. This function can accept parameters through either GET or POST requests and return data in a format such as JSON or XML. To use the function as a web service, you can call it from client-side code using AJAX or other HTTP request methods.

<?php

// Function to be used as a web service
function getWeatherData($city) {
    // Code to fetch weather data for the given city
    $weatherData = array(
        'city' => $city,
        'temperature' => '25°C',
        'conditions' => 'Sunny'
    );
    
    return json_encode($weatherData);
}

// Check if a city parameter is provided
if(isset($_GET['city'])) {
    $city = $_GET['city'];
    echo getWeatherData($city);
} else {
    echo 'City parameter is missing';
}

?>