Very
Short Answers
1. What is
file_put_contents() function?
file_put_contents() writes a string to a file. If the file exists,
it overwrites it. If it doesn’t exist, it creates the file and writes content
to it.
2. Define unlink() function?
The unlink() function deletes a file from the system. It
takes the filename as a parameter. If the file exists, it is removed;
otherwise, it returns false.
3. Discuss feof() function?
feof() checks if the end of a file has been reached during
reading. It returns true if the file pointer is at the end of the file, else
false.
4. What is MySQL?
MySQL is a free, open-source relational database management
system. It is used to store and manage data using structured query language
(SQL) and is often used with PHP.
5. What is ODBC?
ODBC (Open Database Connectivity) is a standard API that
allows applications to connect and interact with different database systems
using a common set of commands and functions.
Short Answers
1. What is file handling in
PHP?
File handling in PHP refers to reading, writing, and
manipulating files. PHP provides functions like fopen(), fread(), fwrite(), and
fclose() to work with files. It is used for storing data, logs, or generating
reports without using databases.
2. Explain fwrite() function
in PHP?
The fwrite() function in PHP is used to write data to a
file. It requires a file pointer and the string to write. The file must be
opened using fopen() in write, append, or update mode before using fwrite().
3. What is include()
function?
include() is used to insert the content of one PHP file
into another. It is useful for reusing code like headers and footers. If the
file doesn’t exist, a warning is shown, and script continues.
4. Explain require()
function?
require() includes a PHP file like include(), but if the
file is missing, it causes a fatal error and stops the script. It is used for
essential files like database connection or config files.
5. Syntax to create a table?
CREATE TABLE students (
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(50), age INT
);
This command creates a table named students with three
fields: id, name, and age.
Long
Answers
1. How will you open a file
in different modes? In PHP, files are opened using the fopen() function. It
requires two parameters: the filename and the mode.
The mode determines how the file will be handled. Common
file modes are:
•
'r': Open for reading. File
must exist.
•
'r+': Open for reading and
writing. File must exist.
•
'w': Open for writing.
Creates a new file or truncates existing one.
•
'w+': Open for reading and
writing. Truncates existing file or creates new.
•
'a': Open for appending.
Creates file if it doesn’t exist.
•
'a+': Read and append mode.
•
'x': Create a new file.
Error if file exists.
•
'x+': Create and open file
for reading and writing.
Example:
$file = fopen("data.txt", "r");
After completing file operations, the file should be closed
using fclose($file); to free system resources. Always use error handling while
opening files to avoid issues if the file doesn’t exist.
2. Describe fread(),
fgets(), and fgetc() functions in file handling?
PHP offers multiple functions for reading files:
•
fread() reads a specific
number of bytes from a file. It is used to read the complete file or a large
block of text.
Example:
$content = fread($file, filesize("demo.txt"));
•
fgets() reads a single line
from the file until a newline character is encountered. It is used for
line-by-line reading in loops. Example:
$line = fgets($file);
•
fgetc() reads a single
character from the file at a time. It is used when precise control is needed.
Example:
$char = fgetc($file);
These functions are useful based on how much content you
need to read. Combine them with feof() to avoid reading beyond file end. Always
close the file using fclose() after reading.
3. How will you append text
file in file operations?
Appending data means adding new content to the end of an
existing file without overwriting it. To append in PHP, the file must be opened
in 'a' (append) or 'a+' (append with read) mode using fopen().
Example:
$file = fopen("log.txt",
"a"); fwrite($file, "New log entry.\n"); fclose($file);
•
fopen() opens the file in
append mode.
•
fwrite() adds the new
content at the end of the file.
•
fclose() closes the file to
save changes and free memory.
Appending is useful for maintaining logs, adding records,
or continuous updates without modifying the previous data. You can use
condition checks to ensure the file exists before opening, and file_exists()
function can help avoid errors. Always close the file after appending to ensure
data integrity.
4. How will you establish a
connection to MySQL database?
In PHP, the mysqli_connect() function is used to connect to
a MySQL database. You need to provide server name, username, password, and
database name.
Example:
$conn = mysqli_connect("localhost",
"root", "",
"school"); if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
Explanation:
•
"localhost" is
the server where MySQL runs.
•
"root" is the
MySQL username.
•
"" is the
password (empty in localhost by default).
•
"school" is the
database name.
After connection, you can perform queries using
mysqli_query() and close the connection using mysqli_close($conn);. Always
check if the connection was successful using conditionals. Use proper error
handling to prevent the script from crashing.
5.
How will you insert, delete and append a record in table?
To manage records in a MySQL table using PHP, we use SQL
queries with mysqli_query().
Insert
Record:
$sql = "INSERT INTO students (name, age) VALUES
('John', 20)";
mysqli_query($conn, $sql); Delete Record:
$sql = "DELETE FROM students
WHERE id = 1"; mysqli_query($conn, $sql); Append (Update) Record:
$sql = "UPDATE students SET age = 22
WHERE id = 1"; mysqli_query($conn, $sql);
Inserting adds a new row. Deleting removes an existing row
based on a condition. Updating modifies a row’s data. Always connect to the
database before executing queries and use mysqli_close($conn); at the end. Add
error handling using mysqli_error() for debugging. Use prepared statements for
secure inputs.

0 Comments