Because I don't know enough about making a module for Drupal, and I'm in a bit of a time crunch, I've been including my old site by using PHP Input Filter. It's working fine, however, I just learned that you should use 'return' or 'print' to display data from these types. What exactly is the difference? Why can't I use echo? What happens if I include a .PHP page in the page (include_once 'somepage.php';) that has something like this:

<?php

blah blah blah

?>

<table><tr><td>hooray</td></tr></td>

<?php

blah blah blah

?>

I only do that in a few places, however most of my display is written using 'echo' statements. I just want to make sure I'm not going to be causing myself problems.

Comments

Garrett Albright’s picture

The echo statement will work just fine in place of print. Though there are particular differences between the two statements, as far as Drupal's PHP filter is concerned, they'll both work.

bfreshour’s picture

Do you have any details on how the PHP Input Filter works with regards to processing? Perhaps a link?

I find that if I call exit(); at any point, the page loads only what's in my PHP Input Filter code and nothing else. I had assumed this was because I was sending output before Drupal was -- but if that was the case, wouldn't the output from my code be out of place when it processes correctly (without there being a exit(); command)?

dman’s picture

It's run inside an eval() context. To produce the $content that gets inserted into the page template at a later date.

Specifically, it's wrapped in an output buffer also I believe (ob_get()), which may explain why a premature exit() returns the evaluated code so far ... which is prepared before the page is themed and output.

It's not just run when the page output gets there, it's run earlier ... so you can to things like set the page title, menu, headers, css from your code even,

.dan.
How to troubleshoot Drupal | http://www.coders.co.nz/

bfreshour’s picture

Dan, thanks. That explains it.

bfreshour’s picture

Also, curious as to how putting plain text / HTML in the PHP Input Filter affects it (negatively or at all?)

For example the following in the Body of a story marked as PHP Input Filtered:

<?php
echo "Hello ";
?>
<b>World</b>

Thanks in advance.

Garrett Albright’s picture

I'm not sure how to answer your first question about exit() -- I generally find it unnecessary to halt the execution of PHP code unless an unrecoverable error occurred (or I'm debugging something). As for how plain text is handled, it's handled just as it normally is in PHP -- it's passed along to the output buffer unaffected. In the case of your code above, the output would be

Hello <b>World</b>

Give it a try. For safety's sake, though, I would recommend weaning yourself off of the PHP input filter as soon as possible.

bfreshour’s picture

Definitely need to ween myself off of it, however since I'm in a time crunch, I figured it would be okay for now. Especially since I'm the only one that can create content, but later, if I allow others, I'll only allow the PHP Input Filter for me.

I used exit(); because I had an Ajax request that needed the Drupal session and it wouldn't persist if I simply pointed it to the PHP file. So I used a page with a PHP Input Filter type, including the PHP file the AJAX normally calls, and simply added an 'exit();' after including the file. Worked brilliantly... Here's an example:

<?php
include_once "eir/cp/battleScreenDom.php";
exit();
?>

http://dev.europeinruins.com/battleScreenDom

If you were logged into the site, it would return this instead of the message about not being logged into the site:

a:7:{s:5:"error";s:17:"Invalid action: .";s:8:"joinedID";s:1:"0";s:5:"ready";b:0;s:9:"companies";a:0:{}s:4:"chat";a:2:{s:6:"online";a:1:{i:0;s:7:"fldash2";}s:8:"messages";a:8:{i:0;a:3:{s:4:"time";s:5:"18:03";s:7:"message";s:4:"Test";s:4:"name";s:6:"fldash";}i:1;a:3:{s:4:"time";s:5:"20:49";s:7:"message";s:6:"dsfgdf";s:4:"name";s:6:"fldash";}i:2;a:3:{s:4:"time";s:5:"20:49";s:7:"message";s:13:"dfsgdfgdfgdfg";s:4:"name";s:6:"fldash";}i:3;a:3:{s:4:"time";s:5:"20:50";s:7:"message";s:4:"xxdv";s:4:"name";s:6:"fldash";}i:4;a:3:{s:4:"time";s:5:"21:13";s:7:"message";s:6:"asdfds";s:4:"name";s:6:"fldash";}i:5;a:3:{s:4:"time";s:5:"21:13";s:7:"message";s:7:"asdf233";s:4:"name";s:7:"fldash2";}i:6;a:3:{s:4:"time";s:5:"21:17";s:7:"message";s:8:"sdafasdf";s:4:"name";s:7:"fldash2";}i:7;a:3:{s:4:"time";s:5:"21:17";s:7:"message";s:8:"34123414";s:4:"name";s:6:"fldash";}}}s:11:"openBattles";a:0:{}s:13:"closedBattles";a:0:{}}

The interesting thing is it contains no head/body tags at all... Not sure how Drupal does it... :-/