Create variable name while inside a while loop

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
davidhopkins
Forum Commoner
Posts: 41
Joined: Thu Jun 10, 2010 7:52 am

Create variable name while inside a while loop

Post by davidhopkins »

Hello all.

My code is quite basic, a while loop that echos each line of a file. However i need to explode each line of data into a new array for every line.

My code as it was it

Code: Select all

while (($buffer = fgets($FileHandle, 4096)) !== false) {
            echo $buffer."<br>";
        }
Which just echos each line to the screen.

What i need is as follows

Code: Select all

        while (($buffer = fgets($FileHandle, 4096)) !== false) {
            $line = explode('|',$buffer);
        }
My problem however is $line needs to be changed each time the while loop is run. What would be ideal is if i could add an incrementing number at the end of $line.

Any help would be great :(

Thanks
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Create variable name while inside a while loop

Post by AbraCadaver »

I would use file, but it is basically:

Code: Select all

foreach(file('/path/to/file.txt') as $line) {
   $lines[] = explode('|', $line);
}
print_r($lines);
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Post Reply