Jump to content

Php variable not accessible


Hellos
Go to solution Solved by Barand,

Recommended Posts

$data = file_get_contents($url); // put the contents of the file into a variable
$characters = json_decode($data); // decode the JSON feed

foreach ($characters as $character) {
	//location
	$name = $character->name;
	$region = $character->region;
	$country = $character->country;
	$lat = $character->lat;
	$lon = $character->lon;
	$tz_id = $character->tz_id;
	$localtime_epoch = $character->localtime_epoch;
	$localtime = $character->localtime;



	

}

echo$name;
?>

This is my code and name will not echo the content.


$data = file_get_contents($url); // put the contents of the file into a variable
$characters = json_decode($data); // decode the JSON feed

foreach ($characters as $character) {
	//location
	$name = $character->name;
	$region = $character->region;
	$country = $character->country;
	$lat = $character->lat;
	$lon = $character->lon;
	$tz_id = $character->tz_id;
	$localtime_epoch = $character->localtime_epoch;
	$localtime = $character->localtime;

	echo$name;

	

}


?>

The variable name will output correctly here.

 

Does anyone know what the problem is.

Is it to do with variable scopes like global, static or something?

 

Thanks.

Link to comment
Share on other sites

In your first code block you read each object putting the name in to the same variabe, thus each one overwrites the previous.

Probably, if the last object is empty, or the name is empty, nothing will print.

In the second block you echo each name as it is read.

Without seeing the content of $data or $characters then cannot be sure.

Link to comment
Share on other sites

<?php
//echo $character->is_day . '<br>';

$url = '{"location":{"name":"London","region":"City of London, Greater London","country":"United Kingdom","lat":51.52,"lon":-0.11,"tz_id":"Europe/London","localtime_epoch":1672618641,"localtime":"2023-01-02 0:17"},"current":{"last_updated_epoch":1672618500,"last_updated":"2023-01-02 00:15","temp_c":8.0,"temp_f":46.4,"is_day":0,"condition":{"text":"Light rain","icon":"//cdn.weatherapi.com/weather/64x64/night/296.png","code":1183},"wind_mph":4.3,"wind_kph":6.8,"wind_degree":220,"wind_dir":"SW","pressure_mb":1009.0,"pressure_in":29.8,"precip_mm":0.0,"precip_in":0.0,"humidity":93,"cloud":25,"feelslike_c":7.0,"feelslike_f":44.7,"vis_km":10.0,"vis_miles":6.0,"uv":1.0,"gust_mph":5.8,"gust_kph":9.4}}'; 
$data = file_get_contents($url); // put the contents of the file into a variable
$characters = json_decode($data); // decode the JSON feed

foreach ($characters as $character) {
	//location
	$name = $character->name;
	$region = $character->region;
	$country = $character->country;
	$lat = $character->lat;
	$lon = $character->lon;
	$tz_id = $character->tz_id;
	$localtime_epoch = $character->localtime_epoch;
	$localtime = $character->localtime;

	echo$name;

	

}


?>

Here is the full code.

Thanks

Link to comment
Share on other sites

  • Solution
$data = '{"location":{"name":"London","region":"City of London, Greater London","country":"United Kingdom","lat":51.52,"lon":-0.11,"tz_id":"Europe/London","localtime_epoch":1672618641,"localtime":"2023-01-02 0:17"},"current":{"last_updated_epoch":1672618500,"last_updated":"2023-01-02 00:15","temp_c":8.0,"temp_f":46.4,"is_day":0,"condition":{"text":"Light rain","icon":"//cdn.weatherapi.com/weather/64x64/night/296.png","code":1183},"wind_mph":4.3,"wind_kph":6.8,"wind_degree":220,"wind_dir":"SW","pressure_mb":1009.0,"pressure_in":29.8,"precip_mm":0.0,"precip_in":0.0,"humidity":93,"cloud":25,"feelslike_c":7.0,"feelslike_f":44.7,"vis_km":10.0,"vis_miles":6.0,"uv":1.0,"gust_mph":5.8,"gust_kph":9.4}}';

$characters = json_decode($data, 1); // decode the JSON feed  as array


echo $characters['location']['name'] . '<br>';
echo $characters['current']['condition']['text'] . '<br>';
echo $characters['current']['condition']['code'] . '<br>';

If you want to stay with objects...

$data = '{"location":{"name":"London","region":"City of London, Greater London","country":"United Kingdom","lat":51.52,"lon":-0.11,"tz_id":"Europe/London","localtime_epoch":1672618641,"localtime":"2023-01-02 0:17"},"current":{"last_updated_epoch":1672618500,"last_updated":"2023-01-02 00:15","temp_c":8.0,"temp_f":46.4,"is_day":0,"condition":{"text":"Light rain","icon":"//cdn.weatherapi.com/weather/64x64/night/296.png","code":1183},"wind_mph":4.3,"wind_kph":6.8,"wind_degree":220,"wind_dir":"SW","pressure_mb":1009.0,"pressure_in":29.8,"precip_mm":0.0,"precip_in":0.0,"humidity":93,"cloud":25,"feelslike_c":7.0,"feelslike_f":44.7,"vis_km":10.0,"vis_miles":6.0,"uv":1.0,"gust_mph":5.8,"gust_kph":9.4}}'; 
$obj = json_decode($data);          // decode the JSON feed  as object

echo $obj->location->name . '<br>';
echo $obj->current->condition->text . '<br>';
echo "<img src='{$obj->current->condition->icon}'>" . '<br>';
echo $obj->current->condition->code . '<br>';

image.png.85f214500debefc48865e76f68510507.png

  • Thanks 1
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.