Xajax (any): Tips and Tricks: PHP language tricks






Xajax (any): Tips and Tricks: PHP language tricks

[edit] PHP includes

Make sure that if you have any php includes to remove any newlines after the end of your php ( ?>). At least in php 4.3.x, it inserts these newlines into the text stream and xajax won't process the resulting xml. - GJD 12/12/05

In fact, apparently you can't have any spaces or whitespace after the ?> in any of your PHP files because it could corrupt the XML response output. -- Jared

This affects all sorts of PHP applications that create anything fussier than HTML (GIF, PDF, etc). Leaving exterior whitespace is particularly bad form for include()d/require()d files. A typical consequence for HTML apps is being unable to send headers (including cookies). Also keep an eye out for your error reporting level and display_errors setting. -- David Brown 2006-01-21

If you don't add the final "?>" in your PHP file(s) then the PHP parser will automatically insert a virtual one after the last character. I learnt this little tip years ago and rarely get any of those annoying 'can't send header' error messages anymore. This has been a big, big help with image creation scripts, include files, etc., and now - ajax.

If you're desperate to hold onto your precious "?>" tag, just wrap your include()/require() in with the "ob_start(); ... ob_end_clean();" functions. It's also interesting to note that using include()/require() executes a lot faster than include_once()/require_once() and can speed up the running time of your script if you're using a lot of them.