sorry couldn't frame a better title.
So here's the problem
i have a function inside functions.php

function show_news(){
    $id_counter = 1;
    $json_news = array(
            "id" => 0,
            "title" => ""
        );

    $json_o = json_decode(file_get_contents(JSON_DATA_FOLDER.'news.json'));
    foreach ($json_o as $id => $news_category)
    {
        echo '<h2>'.$id.'<h2>';
        foreach ($news_category as $news)
        {
            if(IsNullOrEmptyString($news->id)){$json_news['id'] = $id_counter; $id_counter++;}
            else{$json_news['id']=$news->id;}
            if(!IsNullOrEmptyString($news->title)){$json_news['title']=$news->title;}

            var_dump($json_news);
            echo "<br/>-------<br/>";
            include('news-layout.php');
        }
    }
}

m reading a json file and for each element i am assigning its value to an array.
then i am including 'news-layout.php'. For testing purposes i've kept just these 3 lines of code inside 'news-layout.php'

<?php 
global $json_news;
var_dump($json_news);
echo"<br/>=======================<hr/>";
?>

so i m doing a var_dump inside my function as well as on the included page. But i am getting strange result. Everything works fine except that var_dump($json_news) on included page shows NULL for first iteration of loop !!
here's the output

todays_specials
array(2) { ["id"]=> int(1) ["title"]=> string(26) "Okie Since I have to do it" } 
-------
NULL 
=======================
array(2) { ["id"]=> int(2) ["title"]=> string(16) "Vegetable Samosa" } 
-------
array(2) { ["id"]=> int(2) ["title"]=> string(16) "Vegetable Samosa" } 
=======================
array(2) { ["id"]=> int(3) ["title"]=> string(16) "Vegetable Pakora" } 
-------
array(2) { ["id"]=> int(3) ["title"]=> string(16) "Vegetable Pakora" } 
=======================

you can see that strange NULL coming there.
can anyone explain what's happening or how to fix it?

Just a quick question.. (I'm very tired so might be missing the point)!

But if I had this code:

<?php 
global $json_news;
var_dump($json_news);
echo"<br/>=======================<hr/>";
?>

It would return NULL because although you are defining $json_news as a global, you're not setting it as anything.. Surely, your function whats to return the array and then you assign the output of the function to this global variable? In other words, your function "show_news()" might return something..

Kinda like this:

<?php 

function show_news(){
     $json_news = array(
                "id" => 0,
                "title" => ""
            );

    return $json_news;
}

global $json_news;

$json_news = show_news();
var_dump($json_news);

?>

// output
// output
array(2) {
  ["id"]=>
  int(0)
  ["title"]=>
  string(0) ""
}

I'm very tired though, might be missing the point!

Member Avatar for diafol

As a rule, functions return a value and this type of function probably needs to do that. Could you give an example of the json data?

@phorce and @diafol : many thanks for helping out. I figured it out, read a little about global variables. inside my function i had to declare the variable $json_news as a global variable. That solved it.

Member Avatar for diafol

Urgh, globals are never good :( You could probably pass the variable as a parameter to the function.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.