Store a variable in a txt file or xml file

I do not have access to a database. Is there a way to store a php or javascript in a permanent txt file or xml file?

Any help would be appreciated

Answers


Direct answer to the question

To answer your question directly (storing one variable) you would do:

$file_path = '/path/to/your/text.file';
$my_var = 'my value';
file_put_contents($file_path, serialize($my_var));

To retrieve the value you would do something like this:

$my_var = '';
$file_contents = file_get_contents($file_path);
if(!empty($file_contents)) {
    $my_var = unserialize($file_contents);
}

By using serialize() you allow your code to store an array or stdClass object as well as numbers and strings in $my_var.

SQLite would be better

If the server supports SQLite then I would recommend using it if you need to store more than one variable.

To interact with SQLite I would recommend using PHPs PDO or perhaps Idiorm.


Need Your Help

Vaadin web application on tomcat creates only one instance of webapp for all users

java vaadin

I've some problems with my English, so I apologize for mistakes.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.