1
echo ("<div style=\"text-align:center\">" . include ('file.php') . "</div>\n");

I'm not sure why, but whenever I put that line in my script I get blank screen :(

hakre
  • 193,403
  • 52
  • 435
  • 836
alexus
  • 7,256
  • 12
  • 44
  • 66

10 Answers10

7
echo "<div style=\"text-align:center\">";
include ('file.php');
echo "</div>\n";

Include does not return the result of the parsing of the file so it makes no sense whatsoever to concatenate here.

Julian Aubourg
  • 11,346
  • 1
  • 29
  • 29
4

When running this piece of code :

echo ("<div style=\"text-align:center\">" . include ('temp-2.php') . "</div>\n");

(Which I copy-pasted from your example, only changing the name of the file)

I get this warning :

Warning: include(temp-2.php</div> ) [function.include]: failed to open stream: No such file or directory

Not sure the () for the include directive are doing what you expect...


Actually, example #4 on the include manual page looks like it might explain this :

<?php
// won't work, evaluated as include(('vars.php') == 'OK'), i.e. include('')
if (include('vars.php') == 'OK') {
    echo 'OK';
}

// works
if ((include 'vars.php') == 'OK') {
    echo 'OK';
}
?>


And, indeed, rewriting your code to this :

echo ("<div style=\"text-align:center\">" . (include 'temp-2.php') . "</div>\n");

Works much better : no warning -- and the file is actually included correctly.


As a sidenote : if you are getting a white screen, it's probably because your configuration "hides" errors and warnings -- which is not quite nice for a development environnement : having those displayed would halp you quite a bit.

To change that, you can use the error_reporting and display_errors options in your php.ini file.

Or, if you don't have access to that one, you can use error_reporting and ini_set -- something like this at the beginning of your script might do :

error_reporting(E_ALL);
ini_set('display_errors', 'On');

Note : of course, you don't want that on your production environment, on which you should log errors (see log_errors), and not display them.

Pascal MARTIN
  • 395,085
  • 80
  • 655
  • 663
  • Finally someone who explains what the problem was, not just giving a solution. +1 – MitMaro Sep 22 '09 at 21:19
  • @MitMaro : thanks ! (Actually, it was fun : once again, going through a question on SO, I've had an occasion to learn something -- I had never tried this kind of include before ^^ ) – Pascal MARTIN Sep 22 '09 at 21:21
  • 1
    Note that the contents of `include` will appear before those of `echo`. So the contents of `include` are *not* wrapped by the DIV. – Gumbo Sep 22 '09 at 21:29
  • @Gumbo : thanks for poiting this out ! Didn't notice it :-( (Apparently, you can get something inside the div, if it's returned, via a return statement, though -- with a bit of output buffering, there might be something one can do... Even if I wouldn't go with that solution) – Pascal MARTIN Sep 22 '09 at 21:42
  • @Pascal MARTIN: I know, I wrote that in my answer. ;) – Gumbo Sep 22 '09 at 21:49
1

I dont know if you can do such a thing. Do the include first and then do the echo of any given variable from the include file.

Elzo Valugi
  • 27,240
  • 15
  • 95
  • 114
  • i thought about that and i'm pretty sure it'd work, but i kind of want a 1 liner here... besides working around the issue its not exactly solving this issue – alexus Sep 22 '09 at 21:10
  • php echo is a construct is not a function. When calling include the included file is executed. – Elzo Valugi Sep 22 '09 at 21:12
  • you probably have error_reporting set to off. that is why you dont get an error – Elzo Valugi Sep 22 '09 at 21:13
1

include is not a function but a special language construct. That’s also the reason why you don’t need the parenthesis around the “parameter” as include only has one “parameter”. Wrapping that in parenthesis is like wrapping any other value in parenthesis:

1 === (1) === ((1)) === (((1))) === …

Instead you need to wrap the whole construct in parenthesis:

echo "<div style=\"text-align:center\">" . (include 'file.php') . "</div>\n";

But since include does not return the output of the included script file but prints it directly, you would need to buffer the output and return it to the script that included that file with:

<?php // file.php
    ob_start();
    // …
    return ob_get_clean();

That would work. But I don’t think that you want to do that. So this is probably quite easier:

echo "<div style=\"text-align:center\">";
include 'file.php';
echo "</div>\n";

By the way: echo, print, exit and die are special language constructs too. See also require_once () or die() not working

Community
  • 1
  • 1
Gumbo
  • 643,351
  • 109
  • 780
  • 844
  • that i tried and it worked fine for me, so there is no way for me to make a 1 liner out of this? – alexus Sep 22 '09 at 21:14
  • @alexus: In PHP the statements are separated by semicolons and not by line breaks like in other languages. So you can put that three statements in one line, if you want to. That will give you a one liner: `echo "
    "; include 'file.php'; echo "
    \n";`
    – Gumbo Sep 22 '09 at 21:17
  • 3
    I really don't understand peoples need for one liners. Most of the time they just make the code harder to read. – MitMaro Sep 22 '09 at 21:29
1

I think you would need to do something like this:

ob_start();
include('file.php');
$contents = ob_get_clean();

echo ("<div style=\"text-align:center\">" . $contents . "</div>\n");

the "include" directive doesn't return anything, so trying to append it to a string is not going to work.

Eric Petroelje
  • 59,820
  • 9
  • 127
  • 177
0

to output the contents of file.php you'll probably want to use http://us3.php.net/file_get_contents

John Boker
  • 82,559
  • 17
  • 97
  • 130
0

Is there anything in file.php? It may be empty, or something in it may be causing an error.

Donut
  • 110,061
  • 20
  • 134
  • 146
0

Look at the source code in your browser, not just the visible part. Usually you can do this by pressing Ctrl+U. If it was just outputting an empty div element, you would see a blank screen in the browser. And keep in mind that .php files are most likely parsed in your setup before being output, which means you wouldn't ever see <?php ?> tags and the code inbetween them.

slikts
  • 8,020
  • 1
  • 27
  • 47
0

I never thought of include as a function which returns a string you could print.

The Reference Manual suggests that, if anything, it returns a boolean value.

pavium
  • 14,808
  • 4
  • 33
  • 50
-1

Is there an extra line break at the end of file.php?

deadcyclo
  • 112
  • 2
  • 7