Basic-Level PHP Questions
Basic-Level PHP Questions
🔹 1. What is PHP?
PHP stands for Hypertext Preprocessor. It is a server-side scripting language used to develop dynamic and interactive web pages.
🔹 2. When and why is PHP used?
PHP is used for server-side development, such as handling forms, sessions, database operations, file uploads, and more. It’s widely used because it's:
-
Easy to learn
Open-source
-
Platform-independent
-
Supports multiple databases
🔹 3. What are the key features of PHP?
Open-source
-
Server-side scripting
-
Cross-platform (Windows, Linux, macOS)
-
Embedded in HTML
-
Supports many databases (MySQL, PostgreSQL, etc.)
-
Large community support
-
Loosely typed language
🔹 4. What is the latest version of PHP?
As of now (2025), the latest stable version is PHP 8.4. (Note: Always check php.net for the latest version.)
🔹 5. How do you write a simple PHP script?
<?php
echo "Hello, World!";
?>
🔹 6. What are variables in PHP?
Variables in PHP store data and are declared using the
$ symbol:$name = "John";
🔹 7. What are data types in PHP?
PHP supports several data types:
Data Type Example Description - String
- "Hello"
- Sequence of characters
- Integer
- 10
- Whole number
- Float
- 10.5
- Decimal number
- Boolean
- true / false
- Logical values
- Array
- [1, 2, 3]
- Collection of values
- Object
- new ClassName()
- Instance of a class
- NULL
- NULL
- No value
- Resource
- mysqli_connect()
- External resource (e.g., database)
🔹 8. What is the difference between echo and print in PHP?
echocan take multiple parameters, slightly faster, no return value.
printcan only take one parameter and returns1, so it can be used in expressions.
echo "Hello", " World"; // Valid
print "Hello"; // Valid
🔹 9. What is the difference between == and === in PHP?
==checks for value equality (type conversion allowed).===checks for value and data type equality (strict comparison).
5 == "5" // true
5 === "5" // false
🔹 10. What is the difference between single (') and double (") quotes in PHP?
- Single quotes
' 'do not parse variables. - Double quotes
" "parse variables and escape sequences.
$name = "John";
echo 'Hello $name'; // Hello $name
echo "Hello $name"; // Hello John
🔹 11. How can you define a constant in PHP?
using define key
define("SITE_NAME", "TransSunshine");
echo SITE_NAME; //
🔹 12. What are the different types of arrays in PHP?
- Indexed arrays: Numeric keys
<?php
$fruits = ["Apple", "Banana", "Mango"];
echo $fruits[0]; // Output: Apple
?>
- Associative arrays: Named keys
<?php
$person = [
"name" => "Lakshmi",
"age" => 27
];
echo $person["name"]; // Output: Lakshmi
?>
- Multidimensional arrays: Arrays within arrays
<?php
$students = [
["Lakshmi", 27],
["Rahul", 25]
];
echo $students[0][0]; // Output: Lakshmi
?>
13. What is the difference between indexed and associative arrays?
- Indexed arrays use numeric indexes.
- Associative arrays use named keys.
14. How do you loop through an array in PHP?
There are many ways, but the most common and easiest is:
foreach loop$fruits = ["Apple", "Banana", "Mango"];
foreach ($fruits as $fruit) {
echo $fruit . "<br>";
}
Another way:
for loop (if you want to use indexes)$fruits = ["Apple", "Banana", "Mango"];
for ($i = 0; $i < count($fruits); $i++) {
echo $fruits[$i] . "<br>";
}
🔹 15. What are the different types of loops in PHP?
while-
do...while -
foreach
🔹 16. How do you define a function in PHP?
function greet($name) {
return "Hello, $name";
}
🔹 17. What are PHP superglobals? Can you name a few?
Superglobals are built-in variables accessible from anywhere:
$_GET-
$_POST -
$_REQUEST -
$_SESSION -
$_COOKIE -
$_FILES -
$_SERVER -
$_ENV
🔹 18. What is the difference between $_GET and $_POST?
$_GET: Data is sent via URL, visible, limited length.$_POST: Data is sent in the body, secure, supports large data (like file uploads).
🔹 19. How does PHP handle forms (GET vs POST)?
PHP retrieves form input using:
$name = $_GET['name']; // For method="GET"
$name = $_POST['name']; // For method="POST"
🔹 20. What is the use of isset() and empty() in PHP?
isset(): Checks if a variable is set and not NULL.empty(): Checks if a variable is empty (0, "", NULL, false, etc.)
isset($name); // true if $name exists
empty($name); // true if $name is "" or 0 or NULL
🔹 21. What is the difference between include and require in PHP?
nclude: Gives a warning if the file is missing, and script continues.require: Gives a fatal error and halts(stop) execution if file is missing.
🔹 22. How do you handle errors in PHP?
- Use
try...catchwith exceptions. - Use error reporting functions:
error_reporting(E_ALL);
ini_set("display_errors", 1);
OR
Using
try and catch (Exception Handling) Used for handling critical errors like database/file issues
try {
throw new Exception("Something went wrong!");
} catch (Exception $e) {
echo "Error: " . $e->getMessage();
}
23. How do you start a PHP session?
session_start();
$_SESSION["user"] = "John";
24. What is the difference between cookies and sessions in PHP?
- Cookies: Stored on the client-side (browser), persist across sessions.
- Sessions: Stored on the server-side, expire when the browser closes (or after timeout).
Comments
Post a Comment