PHP Beginner Questions Interview Preparation
Level Type
Beginner Basics (syntax, variables, data types, operators, loops, functions)
1. What is PHP?
➤ PHP is a server-side scripting language used to create dynamic web pages.
2. How to declare a variable in PHP?
➤ Using $
$name = "Lakshmi";
3. What are PHP data types?
➤PHP data types are like Integer, Float, String, Boolean, Array, Object, NULL, and Resource — used to define what kind of data a variable stores.
➤You can check a variable's data type using
var_dump($variable);
➤ String, Integer, Float, Boolean, Array, Object, NULL.
4. How do you write comments in PHP?
➤ Single-line:
//
, multi-line: /* comment */
5. What is the difference between
echo
and print
?➤ Both
echo
and print
are used to display output in PHP➤ echo can output multiple values, faster.
print
returns 1, usable in expressions.6. How to write a function in PHP?
➤ A function in PHP is a reusable block of code that performs a specific task and is defined using the
function
keyword.➤ In PHP, a function is created using the
function
keyword, followed by a function name, parentheses ()
, and curly braces {}
containing the code to runfunction greet($name) {
return "Hello $name";
}
7. What are constants?
➤ Defined using
define()
, values that don’t change.8. What is a loop?
➤ Used to execute code repeatedly:
for
, while
, foreach
, do...while
9. What is the difference between
==
and ===
?➤
==
checks only value, ===
checks value and data type both .👉 Always use
===
when you want strict comparison (especially when working with login/authentication, form validations,
10. How to include a file in PHP?
➤
include 'file.php';
or require 'file.php';
11.What is the use of
isset()
?➤
isset()
Checks if a variable is set and not null.12. What is
empty()
?➤ It's checks is variable is empty or not. Returns true if a variable is empty.
13. What are superglobals in PHP?
➤ Built-in variables:
$_GET
, $_POST
, $_SESSION
, $_FILES
, etc.14. What is the difference between
$_GET
and $_POST
?➤
GET
: data in URL, visible; POST
: secure, in body.$_GET
sends data via URL, is visible, less secure, and good for search or pagination.$_POST
sends data hidden in the request body, is more secure, and ideal for forms like login or registration.
15. How to create an array?
➤ An array in PHP is a variable that can hold multiple values at once.
You can create it using
You can create it using
array()
or short syntax []
.$arr = array("one", "two", "three");
16 . What is the
die()
function?➤ Terminates script execution and shows an error message.
17. How do you concatenate strings in PHP?
➤ Using the dot (
.
) operator:$full = $first . $last;
18. What are magic constants?
➤Magic constants in PHP are special predefined constants that give information about the file, line, function, class, or method dynamically. They are very useful for debugging and meta-programming.
- __LINE__
- __FILE__
- __DIR__
- __FUNCTION__
- __CLASS__
- __METHOD__
- __NAMESPACE__
They start and end with double underscores (
__
) and provide static info.19. What is
var_dump()
used for?➤ Displays detailed variable info (type + value).
20.What is a NULL value in PHP?
➤ A variable with no value assigned.
21. What is a PHP array?
➤ An array in PHP is a special variable that can store multiple values under one name
22. What is the use of the $_POST variable in PHP?
➤$_POST
is used to get data from an HTML form when the form's method is POST.23. What are the different types of loops in PHP?
➤PHP supports
for
, while
, do...while
, and foreach
loops to repeat code blocks based on conditions or array data.24. What is the purpose of the break statement in PHP?
➤ break
is used to terminate a loop or switch case early when a certain condition is met.25. How do you connect to a MySQL database in PHP?
➤ In PHP, you can connect to MySQL using
mysqli_connect()
by providing server, username, password, and database name.26. How do you fetch data from a MySQL database in PHP?
➤ We fetch data using
mysqli_query()
to run the SQL SELECT statement, and mysqli_fetch_assoc()
to get rows as an associative array.27. What is a PHP session?
➤ A session stores user data on the server across multiple pages using a unique session ID
28. What is the use of the $_GET variable in PHP?
➤
$_GET
is used to collect form data or information passed via the URL in PHP.29. What are superglobals in PHP?
➤ Superglobals are special global arrays in PHP used to access data like form inputs, server info, sessions, and cookies from anywhere in the script.
$_GET
— Data from URL parameters.$_POST
— Data from HTML forms (POST method).$_REQUEST
— Data from GET, POST, or COOKIE.$_SESSION
— Data stored in user session.$_COOKIE
— Data stored in cookies.$_FILES
— Information about uploaded files.$_SERVER
— Server and execution environment info.$_ENV
— Environment variables.$_GLOBALS
— Access all global variables.
30. How do you handle file uploads in PHP?
➤ We use
$_FILES
to collect the uploaded file and move_uploaded_file()
to save it to the server.
Comments
Post a Comment