Explain the difference between include
and require
statements
include
includes and evaluates the specified file, and generates a warning if the file cannot be included, but the script will continue execution.
require
also includes and evaluates the specified file, but it generates a fatal error if the file cannot be included, and the script will halt execution.
What are the differences between GET and POST methods?
GET
requests data from a specified resource and appends parameters to the URL. It’s visible in the URL and has length limitations.
POST
submits data to be processed to a specified resource and sends parameters in the request body, which is not visible in the URL and has no size limitations.
How do you handle sessions in PHP?
Use session_start()
to begin a session,
$_SESSION
superglobal to store and access session variables,
and session_destroy()
to end a session.
What is the difference between echo
and print
in PHP?
Both echo
and print
output data to the screen. echo
can take multiple parameters and has no return value. print
always returns 1, so it can be used in expressions, but it only takes one argument.
Explain the purpose of namespace
in PHP.
namespace
is used to encapsulate items such as classes, functions, and constants to avoid name collisions in larger applications.
What is the difference between an interface and an abstract class in PHP?
An abstract class can have both abstract methods (without implementation) and concrete methods (with implementation), and properties.
An interface can only have abstract methods and constants, and a class can implement multiple interfaces but extend only one abstract class.
How does PHP support inheritance?
PHP supports single inheritance where a class can inherit methods and properties from one parent class using the extends
keyword.
Explain the concept of traits in PHP.
Traits are a mechanism for code reuse in single inheritance languages like PHP. A trait is intended to group functionality in a fine-grained and consistent way, and classes can include multiple trait
What are magic methods in PHP? Give examples.
Magic methods are special methods that start with __
and are triggered automatically in certain situations. Examples include
__construct()
__destruct()
__call()
__get()
__set()
__toString()
.
How do you handle errors in PHP?
Errors can be handled using try-catch
blocks for exceptions, custom error handlers using set_error_handler()
, and logging errors with error_log()
.
Explain the difference between try-catch
and custom error handling.
try-catch
is used to handle exceptions, allowing code to catch and handle exception objects. Custom error handling involves defining a custom function to handle errors and registering it with set_error_handler()
for procedural error handling.
What is the purpose of the error_reporting
function?
error_reporting
sets the error reporting level in PHP, allowing you to specify which types of errors, warnings, and notices should be displayed or logged.
How do you read and write files in PHP?
Use functions like fopen()
, fread()
, fwrite()
, and fclose()
to read and write files. file_get_contents()
and file_put_contents()
are also commonly used for simpler file operations.
What is the purpose of the fopen
, fread
, and fwrite
functions?
fopen()
opens a file and returns a file handle, fread()
reads data from an open file, and fwrite()
writes data to an open file.
How do you handle file uploads in PHP?
Use the $_FILES
superglobal to access uploaded files, move_uploaded_file()
to move the uploaded file to a designated directory, and validate the file type and size for security.
How do you connect to a MySQL database using PHP?
Use mysqli_connect()
or PDO
(PHP Data Objects) to establish a connection to a MySQL database.
What are prepared statements, and why are they important?
Prepared statements are used to execute SQL queries with placeholders for parameters. They help prevent SQL injection by separating SQL logic from data, ensuring that user input is properly escaped.
Explain how to perform CRUD operations in PHP with MySQL.
Use SQL queries to Create (INSERT
), Read (SELECT
), Update (UPDATE
), and Delete (DELETE
) records in the database, executed through mysqli
or PDO
.
What are the different data types supported in PHP?
PHP supports several data types including integer
, float
, string
, boolean
, array
, object
, resource
, and NULL
.
How do you define and call a function in PHP?
Define a function using the function
keyword followed by the function name and parentheses. Call a function by its name followed by parentheses containing any arguments.
Explain the concept of variable scope in PHP.
Variable scope determines the accessibility of a variable within different parts of a program. PHP supports global, local, and static scopes, as well as the use of the global
keyword and $GLOBALS
array to access global variables.