void include ( include with no return) [closed]
//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
精彩评论