really, REALLY easy question.

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
Suudsu
Forum Newbie
Posts: 9
Joined: Sun Nov 23, 2008 2:55 pm

really, REALLY easy question.

Post by Suudsu »

~pickle | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.


Hi, my first time posting.

So, I am just starting off PhP and I am trying to make some code where a loop reads some data in a file, and continues reading until it comes across a blank line in a file. The problem is, nothing prints out. Have fun picking out all errors I have made.

Code: Select all

<?php
$myFile = "file.txt";
$fh = fopen($myFile, 'r');
$Line = fgets($fh)."<br>";
while ($Line != "<br>") {
$All = $All.$Line
$Line = fgets($fh)."<br>";
}
fclose($fh);
echo $All;
?>
file.txt is....

Code: Select all

Hello Sire
I am a squire.
 
Do not see me.

~pickle | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

Re: really, REALLY easy question.

Post by Mark Baker »

Missing semi-colon after

Code: Select all

$All = $All.$Line

Code: Select all

$Line = fgets($fh)."<br>";
while ($Line != "") {
 
$Line will never be != "" because you're always appending "<br>" to it before doing the test, so an empty line in the file will still contain "<br>" at the point when you test it.

Code: Select all

 
<?php
$All = '';
$myFile = "file.txt";
$fh = fopen($myFile, 'r');
$Line = fgets($fh);
while ($Line != "") {
   $All = $All.$Line."<br>";
   $Line = fgets($fh);
}
fclose($fh);
echo $All;
?>
 
 
Suudsu
Forum Newbie
Posts: 9
Joined: Sun Nov 23, 2008 2:55 pm

Re: really, REALLY easy question.

Post by Suudsu »

I actually caught the <br> always being there right and edited it as you posted.
Thanks on the semi-colon thing.

Unfortunately the problem persists, any ideas?
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

Re: really, REALLY easy question.

Post by Mark Baker »

Suudsu wrote:Unfortunately the problem persists, any ideas?
No obvious ones, it works for me.

The only thing I have changed is

Code: Select all

 
while (trim($Line) != "<br>") {
 
because a seemingly blank line may still have spaces in.

Double check that file.txt is in the right directory (the same directory as your PHP), and possibly prefix the file with './' to ensure that it is using the correct directory.

Code: Select all

 
$myFile = "./file.txt";
 
Suudsu
Forum Newbie
Posts: 9
Joined: Sun Nov 23, 2008 2:55 pm

Re: really, REALLY easy question.

Post by Suudsu »

To make sure when I fixed it I didn't mess up anything, this is what I have got....

Code: Select all

<?php
$All = '';
$myFile = "file.txt"
$fh = fopen($myFile, 'r');
$Line = fgets($fh)."<br>";
while (trim($Line) != "<br>") {
   $All = $All.$Line;
   $Line = fgets($fh)."<br>";
}
fclose($fh);
echo $All;
?>
Could you post the code that works for you, so I can see if its a problem with the code or not?
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

Re: really, REALLY easy question.

Post by Mark Baker »

Missing semi-colon after

Code: Select all

$myFile = "file.txt"
Suudsu
Forum Newbie
Posts: 9
Joined: Sun Nov 23, 2008 2:55 pm

Re: really, REALLY easy question.

Post by Suudsu »

Success! Thanks a ton.
Post Reply