peter11 Posted June 4, 2010 Share Posted June 4, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/203835-file_get_contents-line-by-line/ Share on other sites More sharing options...
syed Posted June 4, 2010 Share Posted June 4, 2010 I you want to store each line into an array you can use the file function as shown below <?php $lines = @file("file.txt"); foreach($lines as $line ){ echo $line; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/203835-file_get_contents-line-by-line/#findComment-1067583 Share on other sites More sharing options...
ignace Posted June 4, 2010 Share Posted June 4, 2010 @syed Remove the @-operator Quote Link to comment https://forums.phpfreaks.com/topic/203835-file_get_contents-line-by-line/#findComment-1067589 Share on other sites More sharing options...
syed Posted June 4, 2010 Share Posted June 4, 2010 why? Quote Link to comment https://forums.phpfreaks.com/topic/203835-file_get_contents-line-by-line/#findComment-1067594 Share on other sites More sharing options...
peter11 Posted June 4, 2010 Author Share Posted June 4, 2010 What i mean is, On that text file i want line to to be saved as $line1 and the second line of that text file to be saved as $line2. Is that possable? Quote Link to comment https://forums.phpfreaks.com/topic/203835-file_get_contents-line-by-line/#findComment-1067602 Share on other sites More sharing options...
ignace Posted June 4, 2010 Share Posted June 4, 2010 why? Because it hides error's the OP would wish to see/handle himself. The @-operator suppresses these and the OP will have a hard time figuring out why it doesn't work. Quote Link to comment https://forums.phpfreaks.com/topic/203835-file_get_contents-line-by-line/#findComment-1067620 Share on other sites More sharing options...
peter11 Posted June 4, 2010 Author Share Posted June 4, 2010 I still don’t get how i would do this is i wanted to assign a separate variable per line. :S Quote Link to comment https://forums.phpfreaks.com/topic/203835-file_get_contents-line-by-line/#findComment-1067623 Share on other sites More sharing options...
Alex Posted June 4, 2010 Share Posted June 4, 2010 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.. Quote Link to comment https://forums.phpfreaks.com/topic/203835-file_get_contents-line-by-line/#findComment-1067624 Share on other sites More sharing options...
syed Posted June 4, 2010 Share Posted June 4, 2010 Yes but, you have to loop throuhg it first, <?php $lines = @file("file.txt"); $i=1; foreach($lines as $line ){ $var["line" . $i] = $line; $i++; } extract($var); echo $line1; echo $line2; ?> Quote Link to comment https://forums.phpfreaks.com/topic/203835-file_get_contents-line-by-line/#findComment-1067626 Share on other sites More sharing options...
ignace Posted June 4, 2010 Share Posted June 4, 2010 @syed Stop posting code which uses the @-operator you hide useful error messages for the OP which they can hide themselves using display_errors = 'Off'. You don't need the @-operator. Quote Link to comment https://forums.phpfreaks.com/topic/203835-file_get_contents-line-by-line/#findComment-1067632 Share on other sites More sharing options...
syed Posted June 4, 2010 Share Posted June 4, 2010 Yes the OP can take it off, are you suggesting that they are not capable of doing this? If the OP wants to see the error messages they can take it off. Quote Link to comment https://forums.phpfreaks.com/topic/203835-file_get_contents-line-by-line/#findComment-1067634 Share on other sites More sharing options...
ignace Posted June 4, 2010 Share Posted June 4, 2010 It's bad practice. Error's are there for a reason. Quote Link to comment https://forums.phpfreaks.com/topic/203835-file_get_contents-line-by-line/#findComment-1067638 Share on other sites More sharing options...
peter11 Posted June 4, 2010 Author Share Posted June 4, 2010 Yer i want to see the error if i get one. Do i want to do it like this to see the error: $lines = file("file.txt"); ?? Quote Link to comment https://forums.phpfreaks.com/topic/203835-file_get_contents-line-by-line/#findComment-1067642 Share on other sites More sharing options...
syed Posted June 4, 2010 Share Posted June 4, 2010 Are you telling me in a live enviroment you would not have the at sign to surpress error messages? Quote Link to comment https://forums.phpfreaks.com/topic/203835-file_get_contents-line-by-line/#findComment-1067643 Share on other sites More sharing options...
syed Posted June 4, 2010 Share Posted June 4, 2010 Yer i want to see the error if i get one. Do i want to do it like this to see the error: $lines = file("file.txt"); ?? Yes, I guess you learned somthing new. Quote Link to comment https://forums.phpfreaks.com/topic/203835-file_get_contents-line-by-line/#findComment-1067645 Share on other sites More sharing options...
ignace Posted June 4, 2010 Share Posted June 4, 2010 Are you telling me in a live enviroment you would not have the at sign to surpress error messages? Indeed. To be honest I have never used the @-operator anywhere on any live site. Quote Link to comment https://forums.phpfreaks.com/topic/203835-file_get_contents-line-by-line/#findComment-1067648 Share on other sites More sharing options...
peter11 Posted June 4, 2010 Author Share Posted June 4, 2010 thanks, I will test it out in a bit and get back to you Thanks Quote Link to comment https://forums.phpfreaks.com/topic/203835-file_get_contents-line-by-line/#findComment-1067649 Share on other sites More sharing options...
peter11 Posted June 4, 2010 Author Share Posted June 4, 2010 yer it works perfect If anyone has time can you explane what each bit is doing please i want to learn it in more detail thanks and thanks Quote Link to comment https://forums.phpfreaks.com/topic/203835-file_get_contents-line-by-line/#findComment-1067663 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.