Jump to content

Recommended Posts

Hi all,

 

My application asks questions that it gets from a random text file however i wish to have like part 1, part 2 and part 3 of each question.

 

At the moment i am using the file_get_contents function and opening the file and echo'ing part 1 of the question. I could easily put part 2 in a different text file however it would also pick that at random and i want part 2 to match up with part one. Therefore what code should i use if i want to have a .txt file with the first part of the question on line 1 the second part on line 2 and the third on line 3. I wish to echo each line into separate a variable.

 

Thanks for the help :)

Link to comment
https://forums.phpfreaks.com/topic/203835-file_get_contents-line-by-line/
Share on other sites

You can do that like this using something like this:

 

$file = file('file.txt');
foreach($file as $key => $line){
    ${'line' . ($key + 1)} = $line;
}

 

But I don't see any need for that. Why create extra variables when you can just access the lines like $file[0], $file[1], etc..

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.