Jump to content

Read a File and Store Whatevers In It Into A Variable


Singularity

Recommended Posts

This must be a stupid question, but what is the code to read a textfile and store it into a variable? Example:

I have a text file called "number.txt" with the number "9" in it.

I have another file called "index.php" that is in the same directory. Using PHP, I want to be able to declare a variable for example "$thenumber" that is equal to whatever number is in the number.txt file.

Please help!
[code]$thenumber=file('number.txt');[/code]


That should do the job mate.. puts it into an array.. to read it back you would have to use somthing like this.

[code]
foreach($thenumber as $number) {
echo $number.'<Br>';
}
[/code]

If you only have 1 line then i beleave it is stored in the variable instead of an array.

Regards
Liam

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.