0
0
PHPprogramming~5 mins

Reading files (fread, fgets, file) in PHP - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the fread function do in PHP?

fread reads a specified number of bytes from an open file pointer. It is useful for reading large files in chunks.

Click to reveal answer
beginner
How does fgets differ from fread?

fgets reads a single line from a file pointer, stopping at a newline or the specified length, while fread reads a fixed number of bytes regardless of lines.

Click to reveal answer
beginner
What is the purpose of the file function in PHP?

The file function reads an entire file into an array, where each element is a line from the file. It is a quick way to get all lines at once.

Click to reveal answer
beginner
What must you do before using fread or fgets?

You must first open the file using fopen to get a file pointer resource.

Click to reveal answer
beginner
Why should you always close a file after reading it?

Closing a file with fclose frees system resources and avoids potential file locks or memory leaks.

Click to reveal answer
Which function reads a whole file into an array, with each line as an element?
Afread()
Bfile()
Cfgets()
Dfopen()
What does fgets() read from a file?
AA single line
BThe entire file
CA fixed number of bytes
DFile metadata
Before using fread(), what must you do?
AClose the file
BNothing, just call <code>fread()</code>
CUse <code>file()</code>
DOpen the file with <code>fopen()</code>
What is the main reason to call fclose() after reading a file?
ATo write to the file
BTo read the file again
CTo free system resources
DTo rename the file
Which function reads a specific number of bytes from a file?
Afread()
Bfile()
Cfopen()
Dfgets()
Explain how to read a file line by line in PHP using fgets.
Think about opening the file, reading lines until no more lines, then closing.
You got /4 concepts.
    Describe the difference between fread and file functions for reading files.
    Consider how much data each reads and how they return it.
    You got /4 concepts.