Skip to content

Instantly share code, notes, and snippets.

@robynitp
Last active August 29, 2015 13:56
Show Gist options
  • Save robynitp/8927124 to your computer and use it in GitHub Desktop.
Save robynitp/8927124 to your computer and use it in GitHub Desktop.
Writing to a file with PHP
<?php
/*
Add a new line to a file that contains records of names and phone numbers
For example, the file might be called clients.txt and look like this:
Chan,Roger,345-9876
Garcia,Victoria,456-1234
Stevens,Julie,678-9872
*/
/* The name and phone number values might eventually come from a form, but here they're hard-coded */
$first = "Tim";
$last = "Thumb";
$phone = "495-2948";
$line = $last.','.$first.','.$phone."\n"; // "Thumb, Tim, 495-2948" + a line break
/* Important!
Use FILE_APPEND to prevent overwriting
*/
file_put_contents('clients.txt',$line,FILE_APPEND);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment