Slight PHP Weirdness

Associate
Joined
21 Sep 2005
Posts
180
Location
Dundee
Well, it's weird to me, but I realise that I am biased and lacking in knowledge...

I have the following bit of code that builds up a string of 3 includes.
PHP:
function redirectPage ($content, $destination, $folderLevel) {

$codeToReturn = '';

	if ($folderLevel ==1) {
		$path = "../res/";
	} 
	else { 
		$path = "../../res/";
	}
	
	$codeToReturn .= include ("../../includes/top_section_redirect.php");


	$codeToReturn .= include ("../../includes/content_redirect.php");


	$codeToReturn .= include ("../../includes/footer.php");

return $codeToReturn;

}
The return from this function is simply echoed to screen.

The code sort of works fine with the exception of one thing. On screen, at the end of the HTML, there are some additional "1's". The number of 1's that are printed after the HTML is always the same as the number of includes. For example, for testing this, if I include the foooter twice, the number of 1's increases to 4 and back to 3 on removal of the additonal footer. The same holds true for any of the includes.

The HTML is shown underneath. All good, except for the trailing 1's.
Code:
/////snip/////

</body>
</html>111

I've tried to google for this but am struggling to find the answer. Any help or insight is much appreciated. :)
 
include() doesn't return the specified file as a string, it just executes it - so you can't append includes to a string variable then attempt to return them. Look into file_get_contents() if the includes are static, else rework your logic.
 
They actually all DO work as intended, I just didn't see the point of showing all the HTML that is returned, just the relevant bit. Only one of the includes is static. The only issue is why the 1's are being added on at the end, 1 for each include, but the includes do append to the string and work.
 
The ones are from the includes being returned. Change
Code:
    $codeToReturn .= include ("../../includes/top_section_redirect.php");


    $codeToReturn .= include ("../../includes/content_redirect.php");


    $codeToReturn .= include ("../../includes/footer.php");

return $codeToReturn;
to
Code:
    include ("../../includes/top_section_redirect.php");


    include ("../../includes/content_redirect.php");


    include ("../../includes/footer.php");
Should work.
 
Back
Top Bottom