📖 Scenario: Imagine you run a small online store. You want to keep a list of products and their prices saved on your computer so you can check and update it anytime. Using file operations in PHP helps you save this list to a file and read it back later.
🎯 Goal: You will create a PHP script that saves a list of products and prices to a file, reads the file content, and shows it on the screen. This teaches you why file operations are important for saving and retrieving data.
📋 What You'll Learn
Create an array called products with exact entries: 'Apple' => 1.2, 'Banana' => 0.5, 'Cherry' => 2.5
Create a variable called filename and set it to 'products.txt'
Write the products array to the file products.txt in a readable format
Read the content from products.txt and store it in a variable called fileContent
Print the content of fileContent to display the saved products
💡 Why This Matters
🌍 Real World
Saving and reading data from files is useful for small apps that need to remember information between runs, like shopping lists or user settings.
💼 Career
Understanding file operations is important for backend developers and anyone working with data storage, backups, or simple databases.
Progress0 / 4 steps
1
Create the products array
Create an array called products with these exact entries: 'Apple' => 1.2, 'Banana' => 0.5, 'Cherry' => 2.5
PHP
Hint
Use PHP array syntax with keys and values like ['key' => value].
2
Set the filename
Create a variable called filename and set it to the string 'products.txt'
PHP
Hint
Use a simple string assignment like $filename = 'products.txt';.
3
Write products to the file
Write the products array to the file named filename in a readable format using file_put_contents and print_r with the second argument true to get a string
PHP
Hint
Use file_put_contents($filename, print_r($products, true)); to save the array as text.
4
Read and display the file content
Read the content from the file named filename using file_get_contents and store it in a variable called fileContent. Then print fileContent to display the saved products.
PHP
Hint
Use $fileContent = file_get_contents($filename); and then print($fileContent);.
Practice
(1/5)
1. Why are file operations important in PHP programming?
easy
A. They improve the speed of PHP code execution automatically.
B. They help in designing the website layout and styles.
C. They are used to create animations on web pages.
D. They allow reading and writing data to files, enabling data storage and retrieval.
Solution
Step 1: Understand the purpose of file operations
File operations let PHP read from and write to files, which is essential for saving data permanently.
Step 2: Identify the correct role of file operations
File operations do not affect website design, animations, or automatic speed improvements.
Final Answer:
They allow reading and writing data to files, enabling data storage and retrieval. -> Option D
Quick Check:
File operations = Data storage and retrieval [OK]
Hint: File operations = saving or loading data from files [OK]
Common Mistakes:
Confusing file operations with styling or animations
Thinking file operations speed up code automatically
Assuming file operations are only for reading, not writing
2. Which of the following is the correct PHP function to open a file for reading?
easy
A. fopen('file.txt', 'r');
B. file_open('file.txt', 'r');
C. openfile('file.txt', 'r');
D. readfile('file.txt', 'r');
Solution
Step 1: Recall PHP file opening functions
The correct PHP function to open a file is fopen, which takes filename and mode as arguments.
Step 2: Check each option's syntax
Only fopen('file.txt', 'r'); is valid syntax. Others are incorrect or do not exist.
Final Answer:
fopen('file.txt', 'r'); -> Option A
Quick Check:
Open file = fopen() [OK]
Hint: Use fopen() to open files in PHP [OK]
Common Mistakes:
Using non-existent functions like file_open or openfile
Confusing readfile() which reads but does not open for writing
Incorrect function syntax or parameters
3. What will be the output of the following PHP code?
The file is opened with mode 'r' which is read-only and does not allow writing.
Step 2: Understand fwrite() requirements
To write, the file must be opened with 'w', 'a', or 'r+' modes. Using 'r' causes an error on fwrite.
Final Answer:
Using 'r' mode prevents writing to the file. -> Option B
Quick Check:
Write needs 'w' or 'a' mode, not 'r' [OK]
Hint: Open file with write mode to use fwrite() [OK]
Common Mistakes:
Opening file in read mode but trying to write
Calling fclose before fwrite
Thinking filename must be absolute path
5. You want to append a new line "New entry" to an existing file "log.txt" without deleting its current content. Which PHP code snippet correctly does this?
hard
A. $file = fopen('log.txt', 'a');
fwrite($file, "New entry\n");
fclose($file);
B. $file = fopen('log.txt', 'w');
fwrite($file, "New entry\n");
fclose($file);
C. $file = fopen('log.txt', 'r');
fwrite($file, "New entry\n");
fclose($file);
D. $file = fopen('log.txt', 'x');
fwrite($file, "New entry\n");
fclose($file);
Solution
Step 1: Understand file open modes for appending
Mode 'a' opens the file for writing at the end, preserving existing content.
Step 2: Check other modes' effects
'w' overwrites the file, 'r' is read-only, 'x' creates new file and fails if exists.
Final Answer:
$file = fopen('log.txt', 'a');
fwrite($file, "New entry\n");
fclose($file); -> Option A
Quick Check:
Append mode = 'a' [OK]
Hint: Use 'a' mode to add without erasing file content [OK]
Common Mistakes:
Using 'w' mode which erases existing content
Trying to write in 'r' mode causing errors
Using 'x' mode which fails if file exists
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.