You are here: Re: Save PHP variables to a text file « PHP Programming Language « IT news, forums, messages
Re: Save PHP variables to a text file

Posted by klenwell on 10/01/07 03:59

On Sep 30, 8:31 pm, Bryan <Bryan.Andr...@gmail.com> wrote:
> On Sep 30, 8:18 pm, klenwell <klenw...@gmail.com> wrote:
>
>
>
> > On Sep 30, 7:30 pm, Lamer <Galat...@gmail.com> wrote:
>
> > > I was wondering how to save PHP variables to a txt file and then
> > > retrieve them again.
>
> > > Example:
>
> > > There is an input box, after submitted the stuff that was written in
> > > the input box will be saved to a text file. Later on the results need
> > > to be brought back as a variable. So lets say the variable is $text I
> > > need that to be saved to a text file and be able to retrieve it back
> > > again.
>
> > > Hope it makes sense,
>
> > > Thanks in advance!!!
>
> > I believe serialize is usually used in cases like this:
>
> >http://www.php.net/manual/en/function.serialize.php
>
> > A while back, I wrote a function called array_smart_dump (yes, the
> > name makes me giggle, too) that accomplishes the same thing with
> > arrays a bit more literally.
>
> > It will convert an array to a string that, as I put it, is
> > syntactically usable, meaning you can write the string (enclosed by
> > appropriate php tags) to file and then later use that array by simply
> > including the file.
>
> > The code can be found here:
>
> >http://klenwell.googlecode.com/svn/trunk/PHP/array/array_smart_dump.i...
>
> > Here's a test script:
>
> >http://klenwell.googlecode.com/svn/trunk/PHP/_unit_tests/test.array_s...
>
> > Tom
>
> What if I want to simply save one result to a text file and be able to
> retrieve it as a variable?

I believe the concept would be the same -- and the execution much
simpler. Something like:

// using heredoc
$file_content = <<<TEXT
<?php

\${$var_name} = {$var_value};

?>
TEXT;

// this would be a user-defined function
write_to_file($fpath, $file_content);

then to retrieve later:

read_file($fpath);

and your variable should be within scope.

Caution with the heredoc above. I don't think the php tags need to be
escaped in any special ways, but test it out -- or use simpler
strings.

Then again, if it's just a single value, why not just write that to
file and then write a simple function to read the file and assign the
return value to the variable you want to use?

Tom

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация