Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6053111
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T08:00:11+00:00 2026-05-23T08:00:11+00:00

I am fairly new to PHP and just trying to convert something I did

  • 0

I am fairly new to PHP and just trying to convert something I did in C. The idiom for reading from a file in C that I was taught was:

while ((c = getchar()) != EOF) {
    doSomethingWith(c);
}

I’ve done some reading and it would seem that the ‘right’ way to do this in PHP is:

while (!feof($file)) {
    $c = fgetc($file);
    doSomethingWith($c);
}

My question is: Is it ok to combine the two as follows (I have tried it, and its working ok on my test file), or are there situations where this could find a null before EOF?:

while (($c = fgets($f)) != null) {
    doSomethingWith($c);
}

Cheers in advance

Steve

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T08:00:12+00:00Added an answer on May 23, 2026 at 8:00 am

    It’s ok, but the way you are doing it it’s wrong (at least "not correct at all"). fgets() returns false, if the end of the file is reached, thus you should test against false instead. It works for you, because you use the "simple" equality operator (==), that will case null to false, when comparing against a boolean.

    Returns a string of up to length – 1 bytes read from the file pointed to by handle. If there is no more data to read in the file pointer, then FALSE is returned.

    If an error occurs, FALSE is returned.

    This means, it have to look like

    while (($c = fgets($f)) !== false) {
        doSomethingWith($c);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I fairly new to JQuery and perhaps trying to achieve something that might be
I'm trying to learn how to use oop in php. I'm also fairly new
I'm fairly new to php and I am trying to figure how do I
I'm new to YQL, and just trying to learn how to do some fairly
fairly new to Objective-C and iOS development (coming from PHP) and I have a
I'm fairly new to PHP, and I just started using NetBeans to develop my
I'm fairly new to PHP and designing a small blog. I'm trying to submit
To put it simply i am a fairly new PHP coder and i was
I'm fairly new to PHP and have built a medium sized website using standard
I'm fairly new to php and was wondering how do I validate the array

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.