Q:

Check file_get_contents return before printing it

So I'm in an enviroment where i cannot use curl so after some research I found that i can use file_get_contents with a stream_context_create context.
It works great, however I noticed that if the request fails file_get_contents returns false, and I wanted to print a JSON object indicating the error instead of plain false.
Here is a snippet of my code:
$postdata = http_build_query(
    $_POST
);

$opts = array('http' =>
    array(
        'method'  => 'POST',
        'header'  => 'Content-type: application/x-www-form-urlencoded',
        'content' => $postdata
    )
);

$context = stream_context_create($opts);

echo file_get_contents($urlAtividade, false, $context);

I use this to get data from another WEBService. My concern here is that some requests have really big outputs, so I'm wondering about what's the best way to check this.
I would like to know if I save the return in a variable, would the file_get_contents return be doubled in memory? Because my biggest concern here is creating overhead or running out of memory.
$return = file_get_contents($urlAtividade, false, $context);

if($return === FALSE){
    $response["error"] = TRUE;
    $response["error_msg"] = "Request Failed";
    echo json_encode($response);
}
else {
    echo $return;
}

I was wondering if there is a way to print the result and return false inside the if statement?

A:

I don't think you need to worry about this.
Assigning a value to a variable doesn't make a copy of it, it just creates a reference to the same memory. Copies are only made if you modify one of them.
If you want to avoid copying the contents into memory, you would have to use readfile() rather than file_get_contents(). This copies the contents directly into the PHP output buffer, which is flushed periodically when it gets full (unless you've turned on output buffering so you can capture it). But if you do this then you won't be able to check the value first.

