PDA

Click to See Complete Forum and Search --> : eronious "?" being rendered



jAQUAN
03-15-2007, 09:04 PM
Here's a chunk of code.


<!--content-->
<div id="php_content">
<?PHP include_once $c ?>
<div style="clear:both;height:0px;overflow:hidden;"><br /></div>
</div>
<!--end content-->

The extra div is a FF hack to get the background to stretch to the visual height.
One of my content pages, which is just text, renders a ? between the included content and the hack div. In IE it renders that missing character square.

When tested locally in xampp, it works fine, but on my server who's running the latest php's, it renders the "?".

I've scoured my code for a possible goof on my part but its fine and like I said, it's only that one page and every page is loaded the same.

catbert303
03-16-2007, 09:27 AM
Hmm, strange. Question marks appearing in a page are usually an encoding - or possibly a font - issue. Here's a few ideas about what could be causing it and some possible solutions for each case.

Perhaps there is some character appearing in that particular PHP file that can't be displayed using the encoding for your page. Depending on how the file was saved (the encoding) this could be something like a byte order mark (if the file was saved using a unicode encoding) or possibly just some random garbage character that your editor has helpfully included for you.

First check the encoding of the PHP file. If it isn't unicode, then a byte order mark shouldn't be present (and hence isn't the problem) but perhaps the file has picked up some other character - try copying its content into a new file and saving a fresh copy to use. That might solve things.

If unicode is being used and the content of the file doesn't require any characters outside us ascii don't save it using unicode (e.g. in Windows notepad select ANSI from the encoding options on the save as dialog) in most other editors you should have menu options for toggling the encoding.

However if you do need to be using unicode in that file, you need a way to stop the hidden characters being output to the browser. This might be one way to do it. Modify the include file so that instead of outputting content directly it contains a function that when called returns the content,


function aBitOfContent() {
return '<p>The content for the page</p>';
}

then include this file right at the start of your PHP script. Now the only thing being output to the browser from the file should be the garbage characters that you want rid of. To do that you can use output buffering - http://uk2.php.net/manual/en/ref.outcontrol.php



<?php

ob_start(); // start buffering the output
include_once('your_file.php');
ob_end_clean(); // discard anything that was output by the include statement

// more code here

?>
<div id="php-content">
<?php
echo aBitOfContent(); // call the function from the file to get the content
?>
<div style="clear:both;height:0px;overflow:hidden;"><br /></div>
</div>


Hope something here helped :)

jAQUAN
03-16-2007, 02:29 PM
nicely done man! Exactly the post I was looking for. I did a save as out of textpad instead of DW and it worked. They were already ANSI files so I didn't change format by doing that.
Btw, thanks for suggesting the function work around, I never would have thought of that and I'm sure it will come in handy for something.

Is using output buffering like that a good idea in all cases when using include/include_once or just for specific uses?

catbert303
03-17-2007, 06:48 AM
No problem :)

The output buffering (and then discarding the buffer) around the includes would only be needed in a case like this where the include file was outputting some character you didn't want to be output.

However, output buffering in general can be very useful. The main idea is that instead of being sent directly to the browser all output is written to a buffer instead - this means at any point in your script you can safely use things like the header function (which would normally fail if any content had already been sent) then at the end of the script you flush the buffer to send the content to the browser.

One of the most useful features of it is that you can apply callback functions to modify the content of the buffer. For example your site might have a glossary section that defined common terms used on the site - using a callback function you could search through the content of the for occurences of those terms and automatically link them to the definition in the glossary.

jAQUAN
03-18-2007, 12:43 AM
very cool. thanks again.