$file = include("file.php"); ?

T

TheJkWhoSaysNi

Guest
is it possible? whenever i try that, it includes it where the variable is defined :( can anyone help?
 
J

Jonty

Guest
Unfortunately I'm not well versed in the theory behind PHP, but as far as I am aware you cannot assign a variable in the context you mention. The include() function is just that, a function which is intended to be executed as soon as it is encountered. You have to remember that include() is essentially designed to import information contained within multiple documents so that code in one page can be successfully executed.

Perhaps if you could enlighten us as to why you are wanting to assign a variable to include(), then we may be able to figure out some better way of achieving what you wish to do.

Like I say, though, the above is me just stumbling around in laymen's terms trying to express what I have encountered. If you want a proper explanation and/or solution, then http://www.php.net or the http://www.phpbuilder.com forums may be a good place to try. But please feel free to post here first, and, whatever the case, let us all know how you get on.

Kind Regards
 
T

TheJkWhoSaysNi

Guest
i'm trying to generate a RSS news feed, from my current news script
Code:
$somecontent = '
<!DOCTYPE rss PUBLIC "-//Netscape Communications//DTD RSS 0.91//EN" "http://my.netscape.com/publish/formats/rss-0.91.dtd">
 <rss version="0.91">
 <channel>
  <title>UD News</title>
  <link>[url]http://www.ud.barrysworld.net.com[/url]</link>
  <description>UD News and site news</description>
  <language>en-us</language>
  <copyright>Copyright Xero, 1998-2002</copyright>
  <managingEditor>Xero</managingEditor>
  <webMaster>thejkwhosaysni@evilemail.com</webMaster>' . include("http://www.ud.barrysworld.net/board/index.php?action=newstrillian;board=4;template=rssnews_template") . '</channel> </rss>';

if (is_writable($filename)) {

     if (!$fp = fopen($filename, 'a')) {
         print "Cannot open file ($filename)";
         exit;
    }

.
    if (!fwrite($fp, $somecontent)) {
        print "Cannot write to file ($filename)";
        exit;
    }
    if (!fwrite($fp, $somecontent)) {
        print "Cannot write to file ($filename)";
        exit;
    }

    print "Success, wrote ($somecontent) to file ($filename)";

    fclose($fp);

} else {
    print "The file $filename is not writable";
}
 
T

Teaser

Guest
The stuff in the filename you give include () will be opened as soon as you use include (). That's just the way it works.

There are 2 possible return values of include ():

1 - true or false
2 - whatever you want


Go to: http://www.php.net/manual/en/function.include.php . At the bottom of this page, it explains about the return value of include () .

PHP isn't fussy where you use include () (or require()) so use it where you need it if you really wish to (like in a function - but read up about the relationship between the scope of the function and include () in that URL above).
 
J

Jonty

Guest
Just out of curiousity, I understand your what you're trying to do, but the question remains as to why you need to use something like $file = include('file.php')?
 
T

TheJkWhoSaysNi

Guest
i dont need it quite like that, i need it inside the $somecontent variable.
 
S

Shocko

Guest
So you basically want to execute the script, and put the resulting html into a variable so it can be written to a file? I think you might be best treating it like a remote file, and using the standard file functions.

$filename = "http://www.ud.barrysworld.net/board/index.php?action=newstrillian;board=4;template=rssnews_template";
$fp=fopen($filename,"r");
if($fp){
get content and stick it into $content
}

$somecontent = "blah whatever" . $content . "whatever else";

I think that's what you want :p
 
M

Mr. 47

Guest
the file thats included must be plain HTML, it won't run any PHP contained within as by this time its passed that bit...
 

Users who are viewing this thread

Top Bottom