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 :(
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 :(
to output the contents of file.php you'll probably want to use http://us3.php.net/file%5Fget%5Fcontents
Is there anything in file.php
? It may be empty, or something in it may be causing an error.
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.
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.
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.
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.
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
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.
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.