开发者

void include ( include with no return) [closed]

It's difficult to tell what is bei开发者_JAVA百科ng asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 12 years ago.
//include.php  
<?php  
echo 'ABC';  
?>

//buzz.php  
<?php
$a = include('include.php);
echo $a
?>

-> Output: ABC1. give me a solution (i know why, needn't explain);


Output buffering might be what you're looking for:

//include.php  
<?php  
echo 'ABC';  
?>

//buzz.php  
<?php
ob_start()
include('include.php'); // Added the closing quote, it's missing in your example
$a = ob_get_clean();

echo $a; // ABC
?>


echo will output to the output buffer, so if you call echo once in include.php then you don't need to try to call it again in buzz.php.

include() will return 1 if the files exists and 0 if it doesn't so you're code will output ABC when include.php is run, then it will print 1 when include('include.php') is called, since the file does exist.

To only print ABC follow Mike's advice or the simpler:

//include.php  
<?php  
echo 'ABC';  
?>

//buzz.php  
<?php
include('include.php);
?>

Finally, you can return from files;

//include.php  
<?php  
return 'ABC';  
?>

//buzz.php  
<?php
$a = include('include.php');
echo $a;
?>


Your problem is this:

$a = include('include.php);

include() will return boolean TRUE if the include succeeded, so $a becomes TRUE, which is cast to a 1 when you output it.


Simply use return

//include.php
return 'ABC';

//buzz.php
$data = include('include.php');
echo $data;  // ABC
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜