PHP Interview Questions and Answers

This section covers a wide range of PHP interview questions, from fundamental concepts to more advanced topics in file handling, database interactions, error handling, and security.

PHP Sessions

A session in PHP is a way to store information about a user across multiple page requests. A unique session ID is used to identify the user's session. The session ID is typically stored in a cookie on the client-side and a session data on the server-side. The session ends when the browser is closed, or the server times out the session.

session_start() and session_destroy()

session_start() initiates a PHP session (creates a new one or resumes an existing one). session_destroy() ends the current session by deleting the session data on the server-side.

More details on PHP sessions

Sessions vs. Cookies

Feature Session Cookie
Storage Location Server-side Client-side (browser)
Data Storage Can store multiple variables Stores only string data
Expiry Typically ends when the browser closes or times out Can be set to expire at a specific time

Opening Files in PHP

The fopen() function opens a file or URL.

Syntax

$fileHandle = fopen($filename, $mode); 

More details on fopen()

Reading Files in PHP

PHP offers several functions for reading files:

  • fread(): Reads a specified number of bytes.
  • fgets(): Reads a single line.
  • fgetc(): Reads a single character.

More details on file reading functions

Writing to Files in PHP

The fwrite() and fputs() functions write data to a file. Make sure you open the file in the appropriate mode (w, a, etc.).

More details on file writing functions

Deleting Files in PHP

Syntax

unlink($filename);

More details on deleting files

Running PHP Scripts from the Command Line

Command

php myScript.php

Uploading Files in PHP

Use move_uploaded_file() to move an uploaded file from the temporary location to a permanent location.

More details on file uploads

Downloading Files in PHP

Use readfile() to send a file's contents directly to the browser for download.

More details on file downloads

Sending Emails in PHP

Syntax

mail($to, $subject, $message, $headers);

More details on sending emails

Connecting to MySQL with PHP

[Explain how to connect to a MySQL database using PHP's MySQLi or PDO extensions. Include examples.]

More details on MySQL connections

Creating Database Connections and Executing Queries

Use mysqli_query() (MySQLi extension) or PDO::query() (PDO extension) to execute SQL queries.

More details on executing queries

Increasing PHP Script Execution Time

Modify the max_execution_time directive in your php.ini file or use the set_time_limit() function within your script.

Types of Errors in PHP

  • Notices: Non-critical errors (usually not displayed to the user).
  • Warnings: More serious errors; the script continues execution.
  • Fatal errors: Critical errors; script termination.

Stopping PHP Script Execution

Syntax

exit(); // or die();

Encryption Functions in PHP

  • crypt()
  • md5()
  • sha1()
  • Other hashing and encryption functions

.htaccess Files

.htaccess files contain Apache server directives for configuring things like URL rewriting, access control, and other settings.

explode() Function

The explode() function splits a string into an array based on a specified delimiter.

split() Function

The split() function splits a string into an array using a regular expression.

Getting the Client's IP Address

PHP Code

$clientIP = $_SERVER['REMOTE_ADDR'];

Persistent Cookies

Persistent cookies remain stored on the client's computer even after the browser is closed. They have an expiry date.

imagetypes() Function

The imagetypes() function returns a bitmask indicating which image types are supported by the GD library.

include() vs. require()

Statement include() require()
Error Handling Issues a warning; continues execution Issues a fatal error; stops execution

Cookies in PHP

Cookies are small pieces of data stored on a user's browser. They're often used to store session identifiers.

[Include a brief explanation of how to create and use cookies in PHP.]

More details on creating cookies

PHP Parser

The PHP parser interprets PHP code. It handles the parsing of PHP code enclosed between the opening and closing PHP tags.

Creating a MySQL Database with PHP

  1. Connect to the MySQL server.
  2. Create the database using an SQL query (e.g., CREATE DATABASE database_name;).
  3. Execute the query using a function like mysqli_query() or PDO->exec().

Further Reading: