Top Interview PHP Questions For TCS
Top Basic PHP Interview Questions and Answers for TCS
Introduction
Preparing for a PHP interview at TCS? PHP (Hypertext Preprocessor) is a widely used server-side scripting language known for its flexibility and efficiency in web development. TCS often tests candidates on core PHP concepts, database integration, object-oriented programming (OOP), error handling, and security practices.
This blog provides a comprehensive list of PHP interview questions to help freshers and experienced developers succeed in their TCS technical interview.
✔ Covers essential PHP topics
✔ Includes practical examples
✔ Suitable for freshers and experienced developers
📌 Bookmark this page and revise these questions before your TCS PHP interview! 🚀
Top Basic PHP Interview Questions and Answers
1. What is PHP?
Answer: PHP (Hypertext Preprocessor) is a server-side scripting language used to develop dynamic web applications. It is open-source and runs on various platforms such as Windows, Linux, and macOS.
2. What are the key features of PHP?
Answer:
- Open-source – Free to use and widely supported
- Server-side execution – Code runs on the server before sending HTML to the browser
- Cross-platform – Runs on multiple operating systems
- Database integration – Supports MySQL, PostgreSQL, SQLite, etc.
- Embedded in HTML – Can be mixed directly with HTML code
3. What is the difference between PHP and JavaScript?
Answer:
Feature | PHP | JavaScript |
---|---|---|
Execution | Server-side | Client-side |
Usage | Backend development | Frontend scripting |
Database Support | Yes | No (requires Node.js) |
4. How do you declare a variable in PHP?
Answer: In PHP, variables start with the $
symbol.
Example:
$greeting = "Hello, TCS!";
echo $greeting;
5. What are PHP data types?
Answer:
- String (
$name = "TCS";
) - Integer (
$age = 25;
) - Float (
$price = 99.99;
) - Boolean (
$isActive = true;
) - Array (
$colors = ["Red", "Green", "Blue"];
) - Object (Created from a class)
- NULL (
$x = NULL;
)
6. How do you write a comment in PHP?
Answer:
// Single-line comment
# Another single-line comment
/*
Multi-line comment
*/
7. What is the difference between echo
and print
in PHP?
Answer:
Feature | echo | |
---|---|---|
Syntax | echo "Hello"; | print "Hello"; |
Return Value | No return value | Returns 1 |
Performance | Faster | Slower |
8. What are PHP superglobals?
Answer: PHP provides built-in superglobal variables:
$_GET
– Collects form data sent via URL$_POST
– Collects form data sent via HTTP POST$_SESSION
– Stores session variables$_COOKIE
– Stores cookies$_FILES
– Handles file uploads$_SERVER
– Contains server and request information
Example:
echo $_SERVER['HTTP_HOST'];
9. How do you connect PHP to a MySQL database?
Answer:
$connection = mysqli_connect("localhost", "root", "password", "database");
if (!$connection) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully!";
10. What is the difference between include
and require
in PHP?
Answer:
Function | include | require |
---|---|---|
Execution | Continues if file is missing | Stops execution if file is missing |
Use case | Optional file | Mandatory file |
Example:
include 'header.php';
require 'config.php';
11. What are PHP sessions?
Answer: Sessions store user data across multiple pages.
Example:
session_start();
$_SESSION["username"] = "TCS User";
echo $_SESSION["username"];
12. What is the difference between isset()
and empty()
in PHP?
Answer:
Function | isset() | empty() |
---|---|---|
Checks | If variable is set | If variable is empty |
Example | isset($x) | empty($x) |
13. How do you handle errors in PHP?
Answer:
try {
throw new Exception("An error occurred");
} catch (Exception $e) {
echo $e->getMessage();
}
14. What is the purpose of the $_FILES
array in PHP?
Answer: It handles file uploads.
Example:
move_uploaded_file($_FILES["file"]["tmp_name"], "uploads/" . $_FILES["file"]["name"]);
15. What are cookies in PHP?
Answer: Cookies store small amounts of data in the user’s browser.
Example:
setcookie("username", "TCSUser", time() + 3600, "/");
echo $_COOKIE["username"];
16. What is the difference between GET and POST methods in PHP?
Answer:
Method | GET | POST |
---|---|---|
Data Sent | URL Parameters | HTTP Body |
Security | Less secure | More secure |
Example | $_GET['name'] | $_POST['name'] |
17. How do you create an associative array in PHP?
Answer:
$employee = ["name" => "John", "age" => 30];
echo $employee["name"];
18. What is object-oriented programming (OOP) in PHP?
Answer: OOP allows using classes and objects.
Example:
class Car {
public $brand;
function setBrand($name) {
$this->brand = $name;
}
}
$myCar = new Car();
$myCar->setBrand("Toyota");
echo $myCar->brand;
19. What is the difference between abstract classes and interfaces in PHP?
Answer:
Feature | Abstract Class | Interface |
---|---|---|
Methods | Can have concrete methods | All methods are abstract |
Properties | Can have properties | Cannot have properties |
Use | Extend with extends | Implement with implements |
20. What is the explode()
and implode()
function in PHP?
Answer:
explode()
– Splits a string into an arrayimplode()
– Joins an array into a string
Example:
$str = "TCS,PHP,Interview";
$arr = explode(",", $str);
echo implode("-", $arr);
21. What is the purpose of header()
function in PHP?
Answer: It sends raw HTTP headers.
Example:
header("Location: welcome.php");
exit();
22. How do you prevent SQL injection in PHP?
Answer: Using prepared statements.
Example:
$stmt = $conn->prepare("SELECT * FROM users WHERE email = ?");
$stmt->bind_param("s", $email);
$stmt->execute();
23. What is the difference between ==
and ===
in PHP?
Answer:
==
checks values===
checks values and types
Example:
var_dump(5 == "5"); // true
var_dump(5 === "5"); // false