tags:

views:

299

answers:

10
+1  Q: 

php echo include

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 :(

A: 

to output the contents of file.php you'll probably want to use http://us3.php.net/file%5Fget%5Fcontents

John Boker
why can't I use include?
alexus
this one will probably print the code in it, I think he wants to execute it
Elzo Valugi
A: 

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

Donut
yes, another php script that works fine on it's own...
alexus
+1  A: 

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
include works fine on it's own if i don't use echo()
alexus
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
php echo is a construct is not a function. When calling include the included file is executed.
Elzo Valugi
you probably have error_reporting set to off. that is why you dont get an error
Elzo Valugi
A: 

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

deadcyclo
no, nothing like that...
alexus
+4  A: 

Hi,

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
Finally someone who explains what the problem was, not just giving a solution. +1
MitMaro
@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
Note that the contents of `include` will appear before those of `echo`. So the contents of `include` are *not* wrapped by the DIV.
Gumbo
@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
@Pascal MARTIN: I know, I wrote that in my answer. ;)
Gumbo
+7  A: 
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
Curses! Beaten to the answer.
EvilChookie
An included file which has "return 'something';" will return that value. If not the include returns 1 (or true) if included successfully, and false if not.
OIS
A: 

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.

Reinis I.
+1  A: 

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 http://stackoverflow.com/questions/1163473/requireonce-or-die-not-working

Gumbo
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
@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 "<div style=\"text-align:center\">"; include 'file.php'; echo "</div>\n";`
Gumbo
I really don't understand peoples need for one liners. Most of the time they just make the code harder to read.
MitMaro
+1  A: 

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
A: 

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