How to store data in PHP without a Database

Data can be stored in many places.

The database is definitely one of the most convenient and secure ways to store data. However, it is not always the fastest and the best.

Everything depends on what you have to store, what’s its size, and most importantly, is it important.

Remember, your critical data (such as passwords) should always be stored with proper hashing methods in your database. Storing this information in your text files may be dangerous.

If you already know the dangers of keeping data out of the database, and you are sure you want to do so, we can move on to the practical part of this tutorial.

You can put your data in files.

You can make your own TXT, XML, or even PHP files to store your data in arrays.

One of the most popular ways is to store your data in normal .txt files separated with commas or new lines.


$yourData = 'something';
file_put_contents('your_file.txt', $yourData);



Save data from the array to the txt file and then read it in another file.

Make the putdata.php file.


$colors = ['red', 'green', 'blue', 'purple', 'black', 'yellow', 'brown', 'pink', 'white'];

$colors = implode(', ', $colors);

file_put_contents('colors.txt', $colors);


Open the file in your browser. It will generate the colors.txt file with the following content:

red, green, blue, purple, black, yellow, brown, pink, white

Now we can read the data from TXT file.



$file = file_get_contents('colors.txt');

echo '<b>Colors:</b>';

$colors = explode(', ', $file);

echo '<ul>';

foreach ($colors as $color) {
    echo '<li>' . $color . '</li>';

echo '</ul>';



  • red
  • green
  • blue
  • purple
  • black
  • yellow
  • brown
  • pink
  • white