2

Possible Duplicate:
php include prints 1

$html = '<div class="subscribe_user"><span id="question1"><a href="#" id="subscribe_link" data-category="'.$category.'">
                Subscribe to comments made on '.$category.'</a></span></div>' . include(SUBSCRIBE_USER_BASE_DIR . '/includes/av_subscribe_form.php');

This include file is an HTML form with some PHP variables.

I'm returning the $html via a plugin:

return $html;

But I cannot get rid of the '1' appended to the output. Of course this means it's a success for inputting the file but how can I work around this?

Community
  • 1
  • 1
AlxVallejo
  • 3,066
  • 6
  • 50
  • 74

6 Answers6

5

Since you probably want the PHP processed and the output stored in the variable, this will do the trick:

ob_start();
include(SUBSCRIBE_USER_BASE_DIR . '/includes/av_subscribe_form.php');
$include = ob_get_clean();

$html = '<div class="subscribe_user"><span id="question1"><a href="#" id="subscribe_link" data-category="'.$category.'">
                    Subscribe to comments made on '.$category.'</a></span></div>' . $include;
Mahn
  • 16,261
  • 16
  • 62
  • 78
  • 1
    Better to fix the problem than hide it I think – vascowhite Jul 27 '12 at 22:01
  • Well it's not always possible to `return` something in a script, eg if you are outputing html directly because you are not using the file exclusively as an include, but also as a standalone script depending on the context. On those cases a method like this is not necessarily bad. – Mahn Jul 27 '12 at 22:05
  • You know, it is hacks like this around bad design that reinforce PHP's reputation as a bad language. It makes me sad. – vascowhite Jul 28 '12 at 07:07
  • @vascowhite I disagree, there are perfectly valid use cases for this, it's a lifesaver for debugging templates for instance. – Mahn Jul 28 '12 at 11:00
3

Okay, using include in combination with return string feels at least a quite unusual way to do this.

First of all imagine this situation:

return.php:

$html = 'blah blah blah';
return $html;

test.php:

$html = 'foo';
$html .= include( 'return.php') . 'bar';

Think about it ;) Your included script should overwrite global variable. You have to be really careful not to overwrite anything when you're doing this.

I'm strongly suggesting that you'll rather use function, classes, plugins for this (so many options) instead of using return $string, but just try renaming variables first.

And are you sure that you're using return and not echo instead (inside form script)? Using just plain html is the same using echo, you have to use return to be able to use it like that, take a look at Example #5 include and the return statement.

Probably file_get_contents('form') is the right solution for you.

Based on your comments:

If you have a file like:

form.php:

<form><blah blah blah></form>

It's equivalent to having:

<?php
echo '<form><blah blah blah></form>';
return 1;

If you'd make it:

<?php
$html = '<form><blah blah blah></form>';

There would be still implicit return 1 at the end.

Vyktor
  • 20,559
  • 6
  • 64
  • 96
  • I'm not using echo nor return in the form script. The included script is just the form html with added php variables where necessary. – AlxVallejo Jul 27 '12 at 22:05
  • @AlxVallejo Ah, therefore it has to do what it does and you should accept `wanovak`'s answer – Vyktor Jul 27 '12 at 22:08
  • @Vyktor I tried assigning the include to a variable and then replacing the return output but I still get the '1' – AlxVallejo Jul 27 '12 at 22:15
1

Edit /includes/av_subscribe_form.php and return its contents. Then it'll return what you want instead of 1 for success.

wanovak
  • 6,117
  • 25
  • 32
0

You should make it like this (if your include returns something, otherwise, just include, don't append):

$html = '...';
$return = include(SUBSCRIBE_USER_BASE_DIR . '/includes/av_subscribe_form.php');
$html .= $return;

This is stated in the PHP documentation too (see Handling returns).

Robin V.
  • 91
  • 5
  • @AlxVallejo From what I read you don't use return nor echo. If that's the case, you simply put `include(SUBSCRIBE_USER_BASE_DIR . '/includes/av_subscribe_form.php');` there. If you want to echo the variable, just `echo $var;` - change `$var` in the variable you're setting in that file. - Edit: never mind, I see you've got what you wanted. – Robin V. Jul 27 '12 at 22:18
0

You are concatenating the result of the include into your string $html.

Your could do something like:

$var = (include 'file.php');
$html = '<div class="subscribe_user"><span id="question1"><a href="#" id="subscribe_link" data-category="'.$category.'">
            Subscribe to comments made on '.$category.'</a></span></div>'. $var;

You cannot assign a variable the value of the function version of include, include();. See example 4.

Mike Mackintosh
  • 13,917
  • 6
  • 60
  • 87
0

include returns 1 if it was successfully included, though in theory it should return the content in cases instead of 1. Check the page for include: http://php.net/manual/en/function.include.php

Is the actual content there? or you get only a "1"?

Also you can do:

ob_start();
include("my_file.html");
$content = ob_get_contents();
echo $output; //prints the html content

To check if anything is written out in the include file

Opi
  • 1,288
  • 1
  • 10
  • 14