为什么echo()输出的base64_decode()无法在浏览器中完全显示?

please see the code below :

<?php
$str = 'PD9waHANCiRzdHJpbmcgPSAiYmVhdXRpZnVsIjsNCiR0aW1lID0gIndpbnRlciI7DQoNCiRzdHIg
PSAnVGhpcyBpcyBhICRzdHJpbmcgJHRpbWUgbW9ybmluZyEnOw0KZWNobyAkc3RyLiAiPGJyIC8+
IjsNCg0KZXZhbCgiXCRzdHIgPSBcIiRzdHJcIjsiKTsNCmVjaG8gJHN0cjsNCj8+IA==';
echo base64_decode($str);
?>

this is a simple php code and you can decode base64 string by using the URL below :
http://www.base64decode.org/

why base64_decode() function in this example can not do it's job. the output is like below :

"; eval("\$str = \"$str\";"); echo $str; ?>  

instead of :

<?php
$string = "beautiful";
$time = "winter";

$str = 'This is a $string $time morning!';
echo $str. "<br />";

eval("\$str = \"$str\";");
echo $str;
?> 

what is the problem and how can i fix it for big codes?

EDIT :
there is an invisible part of output and i can find it by page source.
but why do we have this reaction?
thanks in advance

The problem is that you're viewing it as HTML. If you look at the source or pass a header to make the browser interpret it as text, you'll see the whole thing.

The eval statement just executes the command and returns you the string.... and what you actually require is to eval inside the eval which is kind of absurd.. to happen...

Try view the source of your output - I'm pretty sure the full content is there, but your browser is trying to read it as HTML.

Echoing your base64 encoded PHP script doesn't magically evaluate it; you have to do that yourself:

eval(base64_decode($str));

Did you add those newlines?

If you use the code like this:

<?php
$str = 'PD9waHANCiRzdHJpbmcgPSAiYmVhdXRpZnVsIjsNCiR0aW1lID0gIndpbnRlciI7DQoNCiRzdHIgPSAnVGhpcyBpcyBhICRzdHJpbmcgJHRpbWUgbW9ybmluZyEnOw0KZWNobyAkc3RyLiAiPGJyIC8+IjsNCg0KZXZhbCgiXCRzdHIgPSBcIiRzdHJcIjsiKTsNCmVjaG8gJHN0cjsNCj8+IA==';
echo base64_decode($str);
?>

It works just fine.

But you have added newline (" ") characters in the string. In order to brake it over multiple lines. That's why it doesn't work.

If you need to brake it over multiple lines you have to do this:

<?php
$str = 'PD9waHANCiRzdHJpbmcgPSAiYmVhdXRpZnVsIjsNCiR0aW1lID0gIndpbnRlciI7DQoNCiRzdHIg'.
       'PSAnVGhpcyBpcyBhICRzdHJpbmcgJHRpbWUgbW9ybmluZyEnOw0KZWNobyAkc3RyLiAiPGJyIC8+'.
       'IjsNCg0KZXZhbCgiXCRzdHIgPSBcIiRzdHJcIjsiKTsNCmVjaG8gJHN0cjsNCj8+IA==';
echo base64_decode($str);
?>

If you want to keep those newlines inside the string for example if you have a big base64 encoded string that is already word-wrapped (usually to 70-80 characters per line) you can do the following:

$str = 'PD9waHANCiRzdHJpbmcgPSAiYmVhdXRpZnVsIjsNCiR0aW1lID0gIndpb
        nRlciI7DQoNCiRzdHIgPSAnVGhpcyBpcyBhICRzdHJpbmcgJHRpbWUgbW
        9ybmluZyEnOw0KZWNobyAkc3RyLiAiPGJyIC8+IjsNCg0KZXZhbCgiXCR
        zdHIgPSBcIiRzdHJcIjsiKTsNCmVjaG8gJHN0cjsNCj8+IA==';
$str = implode('', preg_split('/\s*/', $str));
echo base64_decode($str);