Syntax to include a file as a variable???

This is a discussion on "Syntax to include a file as a variable???" within the PHP Forum section. This forum, and the thread "Syntax to include a file as a variable??? are both part of the Program Your Website category.


 Subscribe in a reader

Go Back   Webforumz.com > Main Forums > Program Your Website > PHP Forum

Notices




Reply
 
LinkBack Thread Tools
  #1  
Old Feb 4th, 2008, 19:13
Junior Member
Join Date: Sep 2006
Location: Here
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
Syntax to include a file as a variable???

I'm appending a few lines of text to a file. The text may change later, so I want to make it an include for easy edit.

What is the right syntax to include a file as a variable?

I can declare straight text and it works fine. But how can I include some text from a file?

PHP: Select all

<?php
     $Text 
'I can put text here and it works';

     
$New fopen("$page.php","a");
     
fputs($New"$Text");
     
fclose($New);
?>
Something like this???

PHP: Select all

<?php
     $Text 
= include(this/dont/work.inc);

     
$New fopen("$page.php","a");
     
fputs($New"$Text");
     
fclose($New);
?>
Thanks for any help offered.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote

  #2  
Old Feb 4th, 2008, 19:59
Reputable Member
Join Date: Nov 2007
Location: India
Posts: 150
Blog Entries: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Syntax to include a file as a variable???

You can try
PHP: Select all

<textarea>
<?php
$file
=file("fileAddressHere.php");//returns an array of all lines in that file
$noOfLine=sizeof($files);//returns no. of lines in the file
for($i=0$i<$noOfLine$i++)//Loop to echo out every line of the file
{
echo 
$file[$i];
echo 
"<br>";
}
?>
</textarea>
Last Blog Entry: Cross browser nuisance (Feb 11th, 2008)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #3  
Old Feb 4th, 2008, 20:17
Junior Member
Join Date: Sep 2006
Location: Here
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Syntax to include a file as a variable???

I'm don't know why, but that code only appends the word "Array" to the file.

Is file() a php function? Or do you have to define it as one first?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #4  
Old Feb 4th, 2008, 20:26
Reputable Member
Join Date: Nov 2007
Location: India
Posts: 150
Blog Entries: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Syntax to include a file as a variable???

file() is a PHP function. Are you sure you have copy-pasted the code properly? Esp that [$i] in file[$i]? It works here!
Last Blog Entry: Cross browser nuisance (Feb 11th, 2008)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #5  
Old Feb 4th, 2008, 23:24
Junior Member
Join Date: Sep 2006
Location: Here
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Syntax to include a file as a variable???

Pretty sure. But I got the include to work like so...
PHP: Select all

$Text = '<?php include("template/template.php"); ?>';
I think what I may need to solve this is to be able to call a variable instead of an include. Something like...
PHP: Select all

$Text ='$inputText'
I'm writing to the file and I need to be able to change the $Text variable. Maybe dynamically?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #6  
Old Feb 5th, 2008, 10:19
alexgeek's Avatar
Moderator

SuperMember
Join Date: Jul 2007
Location: Webforumz 24/7
Age: 15
Posts: 3,812
Blog Entries: 9
Thanks: 2
Thanked 2 Times in 2 Posts
Re: Syntax to include a file as a variable???

The below is for php5+
If you want to execute PHP code in another script you can do:
PHP: Select all

//;just on it's own where you want it
include('otherfile.php');


//or
$file file_get_contents('otherfile.php');
eval(
$file); 
If you just want to get plain text and not php, you can use:
PHP: Select all

include('otherfile.php'); //put where you want  it to appear

//or
$file file_get_contents('otherfile.php');
// echo $file where you want:
echo $file
That doesn't work for PHP4 because file_get_contents is only available in php5+, you can replace it with fopen/fread/fclose if you wish.
Last Blog Entry: 3D Chess in your browser! (Mar 14th, 2008)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #7  
Old Feb 5th, 2008, 10:44
Rakuli's Avatar
SuperMember

SuperMember
Join Date: Sep 2007
Location: Australia
Age: 24
Posts: 956
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Syntax to include a file as a variable???

Quote:
That doesn't work for PHP4 because file_get_contents is only available in php5+, you can replace it with fopen/fread/fclose if you wish.
__________________
You will actually find file_get_contents is available in PHP 4.3 and later versions. It was considered so useful they gave PHP 4 the function.

file_put_contents is still not available in PHP 4 - it takes the contents of a file and prints it straight out.
Last Blog Entry: The wannabe juggler's quest (Oct 27th, 2007)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #8  
Old Feb 5th, 2008, 11:00
alexgeek's Avatar
Moderator

SuperMember
Join Date: Jul 2007
Location: Webforumz 24/7
Age: 15
Posts: 3,812
Blog Entries: 9
Thanks: 2
Thanked 2 Times in 2 Posts
Re: Syntax to include a file as a variable???

Ah right, thanks for the correction.
Last Blog Entry: 3D Chess in your browser! (Mar 14th, 2008)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Syntax error with Ajax file reading Paramiliar JavaScript Forum 0 Aug 12th, 2007 16:48
How do I add new variable in a .TPL file toasty PHP Forum 1 Oct 9th, 2006 18:27
Session include file manzil PHP Forum 1 Aug 6th, 2006 16:34
Syntax of import/export variable MojoP JavaScript Forum 0 Aug 15th, 2005 17:57
Question about my navigation include file James-Clarke Classic ASP 3 May 17th, 2005 14:36


All times are GMT. The time now is 19:27.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42