How to show daily meteorological conditions?

I am testing. Thank you. It should work.

You don’t need to hard-code “Today” and “Tomorrow” at all, the OpenWeather API already gives you a daily array, so the simplest approach is to loop over it and generate the HTML dynamically.

Basic flow:

  • Call the API and decode the JSON
  • Make sure the response actually contains daily
  • Loop over the first 2 days and output your markup
$response = file_get_contents($url);
$data = json_decode($response, true);

if (!isset($data[‘daily’]) || !is_array($data[‘daily’])) {
echo ‘No forecast data available’;
return;
}

foreach (array_slice($data[‘daily’], 0, 2) as $index => $day) {

$label   = ($index === 0) ? 'Today' : 'Tomorrow';
$maxTemp = round($day['temp']['max']);
$minTemp = round($day['temp']['min']);
$icon    = $day['weather'][0]['icon'];

echo "
<div class='container'>
    <div class='header'>{$label}</div>
    <img src='https://openweathermap.org/img/wn/{$icon}@2x.png'>
    <div>Max: {$maxTemp}°C</div>
    <div>Min: {$minTemp}°C</div>
</div>
";

}

If you’re getting errors like “Cannot access offset of type string on string”, that usually means the API response isn’t what you expect (invalid key, API limit reached, etc.), so always check that daily exists before looping.

Also, regarding dynamic class names: using max(2, $index + 1) will always return 2. If you want the first item styled differently, use a conditional instead:

$class = ($index === 0) ? ‘container1’ : ‘container2’;

This keeps the layout logic simple and avoids unnecessary duplication.