Why am i unable to echo anything on the browser [duplicate]

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

Leave a Comment