Welcome to WebmasterWorld Guest from

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Simple php function

expert help needed

11:10 am on Aug 2, 2007 (gmt 0)

Junior Member

10+ Year Member

joined:Feb 11, 2004
votes: 0

I need a very simple piece of php code that does the following:

INPUT: a string - let's say $a

1. open a .txt file, check if the file already contains a line that equals $a and if so echo on the screen "contains $a"
2. if not, add $a to the end of the file on a new line


11:17 am on Aug 2, 2007 (gmt 0)

Senior Member

WebmasterWorld Senior Member dreamcatcher is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Mar 30, 2003
votes: 0

$string = '$a';

$file = file [uk2.php.net]('file.txt');

if (in_array [uk2.php.net]($string,$file))
// exists
$fp = fopen [uk3.php.net]('file.txt', 'ab');

if ($fp)
fwrite [uk3.php.net]($fp,$string);
fclose [uk3.php.net]($fp);

Not tested, but should give you some pointers.