tags:

views:

65

answers:

4
//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);

+2  A: 

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
?>
Mike B
Wouldn't you, by `include` ing it, not have to `echo` it because it's already `echo`ed in the source file?
esqew
@seanny94 Indeed, but this was my best guess at what Snoob's real-world problem is.
Mike B
This is what i need, thank. @seanny94 : this is just a example:)
Snoob
+2  A: 

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;
?>
Peter Ajtai
i want to make template engine for my project, so this isn't i want.
Snoob
A: 

Simply use return

//include.php
return 'ABC';

//buzz.php
$data = include('include.php');
echo $data;  // ABC
ircmaxell
+1  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.

Marc B
i knew why, needn't explain :)
Snoob