-5

I'm unable to modify the global variable inside the function nor display echo the message on the browser. Please help me

<!DOCTYPE html>
<html> 
<body>
   <?php 
     $response = array(); 
    function() { 
        global $response['res']="hello"; 
        echo json_encode($response); 
     } 
    echo "hello";
    ?>

    </body>
    </html>
  • Your code doesn't echo because it doesn't run because it doesn't compile. `global $response['res']="hello";` is two statements combined into once (this doesn't work). Try `global $response; $response['res']="hello";` – axiac Sep 15 '17 at 08:22
  • You need to name your function and remove `global $response[...]`. – Script47 Sep 15 '17 at 08:23
  • Syntax error: response['res']="hello"; where is `$` – Priya Sep 15 '17 at 08:23
  • 3
    **Don't** modify the code in the question to match the suggestions you get in comments! **Fix the code in your program.** – axiac Sep 15 '17 at 08:24

1 Answers1

0

I've cleaned up your code a little bit and added a name to the function, now I see hello in the browser. The function also needs to return rather than echo, because you can then echo the function like echo test(). This should sort your problem:

<?php
$response = array();
function test() {
    global $response;
    $response['res'] = "hello";
    return json_encode($response);
}
echo test();
?>

Full HTML code that should be placed in a .php file:

<!DOCTYPE html>
<html>
    <body>
        <?php
            $response = array();
            function test() {
                global $response;
                $response['res'] = "hello";
                return json_encode($response);
            }
            echo test();
        ?>
    </body>
</html>

And output in the browser:

enter image description here

Andy Holmes
  • 7,817
  • 10
  • 50
  • 83
  • I saved the file as xyz.html and and inserted the exact code as u have written but i dont get any output on the browser, – Grace Jochu Sep 15 '17 at 08:36
  • this is my whole code

    – Grace Jochu Sep 15 '17 at 08:37
  • 1
    Well you won't if you've saved it as `.html` this code is working as I've tested it locally. You may have other code blocking it. Create a new `.php` file and you'll see the JSON data – Andy Holmes Sep 15 '17 at 08:37
  • okay I have saved the file as .php extension and copied the exact code in the new file created but it still doesnt give any output – Grace Jochu Sep 15 '17 at 08:42
  • As you can see in the answer this is working fine, I can only assume you have a different issue. – Andy Holmes Sep 15 '17 at 08:42
  • Thank you so much, it works now. I did not realize the localhost thing. I was just executing it like a normal html program in my desktop folder. Didn't realise it require php engine – Grace Jochu Sep 15 '17 at 08:48
  • No problem at all @GraceJochu you can also see it working here - http://phpfiddle.org/main/code/0dmq-gfeg just click the run button (f9) :) – Andy Holmes Sep 15 '17 at 08:49
  • @GraceJochu been a while, any chance of an accept on this? – Andy Holmes Jan 18 '18 at 16:04