Jump to content

[SOLVED] why won't this work?


vampke

Recommended Posts

Hi guys,

 

another beginner question I'm afraid....

 

If I enter the code below, as a result I will get only the txt-file includes, but not the raw html input.

 

<?
$header = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">
	<html>
	<head>
	</head>
	<body>";
$header .= include ('incl/top.txt');
$header .= "</tr>
	  <tr>";
$header .= include ('incl/menu.txt');
?>

 

Why won't this work?

Link to comment
https://forums.phpfreaks.com/topic/41441-solved-why-wont-this-work/
Share on other sites

First of all, the include() function does not return a string. It actually treats the included file as part of the current one. So, if I'm understanding what you're after, you want to print out your header and then include the text files. So, if you're looking to actually print out the straight text of those files, you need to use file_get_contents() or another file reading method that does return them as a string:

<?php
$header  = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\n";
$header .= "<html>\n";
$header .= "<head>\n";
$header .= "</head>\n";
$header .= "<body>\n";
$header .= file_get_contents('incl/top.txt');
$header .= "</tr>\n";
$header .= "<tr>\n";
$header .= file_get_contents('incl/menu.txt');
?>

 

Hope that helps.

thanks for quick reply.

unfortunately no luck! now i don't get a return at all.

maybe i should add that the whole file is actually included in another php file which is called.

 

My first try returned the text in the includes, but not the html header text or the </tr><tr> between the 2 includes.

The solution you propose doesn't return anything

don't quite understand the difference between the include and the file_get yet though

 

Think of it this way (it wouldn't hurt to actually try this out): include() makes the included file part of the current file while file_get_contents() returns the contents of the file as a string. This in mind, if you include() a PHP file, the included file will actually be parsed and run just as if the code it contains were part of the parent file. However, if you run file_get_contents() on a PHP file and echo the contents, the actual code of the file would be printed to the screen. Try this out, and it may help with the difference:

 

File 1 (file01.php):

<?php
$greeting = "Hello, how are you";
echo $greeting;
?>

 

File 2 - Include example (file02.php):

<?php
header("Content-type: text/plain");
include("file01.php");
?>

 

File 3 - Get Contents example (file03.php):

<?php
header("Content-type: text/plain");
$file = file_get_contents('file01.php');
echo $file;
?>

 

Notice that in the include() example, the included file is actually run as part of the script, but in the file_get_contents() example, you are pulling the contents of the file as a string.

 

Hope this helps.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.