0% found this document useful (0 votes)
14 views3 pages

PHP V Ques

The document provides a comprehensive overview of PHP, covering its definition, common uses, and key features such as variables, data types, control structures, functions, and object-oriented programming concepts. It also includes information on error handling, database operations, sessions, cookies, and file handling in PHP. Each section outlines essential functions and methods, making it a useful reference for PHP developers.

Uploaded by

Luffy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views3 pages

PHP V Ques

The document provides a comprehensive overview of PHP, covering its definition, common uses, and key features such as variables, data types, control structures, functions, and object-oriented programming concepts. It also includes information on error handling, database operations, sessions, cookies, and file handling in PHP. Each section outlines essential functions and methods, making it a useful reference for PHP developers.

Uploaded by

Luffy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Php

14 November 2024 10:30 PM

1. What is PHP?
○ PHP (Hypertext Preprocessor) is an open-source, server-side scripting language designed
for web development and embedded within HTML.
2. What are the common uses of PHP?
○ PHP is used for server-side scripting, command-line scripting, and creating dynamic web
pages, handling forms, session management, and interacting with databases.
3. What is a PHP file extension?
○ PHP files typically have the .php extension.
4. How do you declare a variable in PHP?
○ In PHP, variables start with a $ sign, followed by the variable name (e.g.,
$variable_name).
5. What are the data types in PHP?
○ PHP supports various data types, including string, integer, float, boolean, array, object,
and NULL.
6. How do you write comments in PHP?
○ PHP supports single-line comments with // or # and multi-line comments with /* ... */.
7. What is the difference between echo and print?
○ Both are used to output data in PHP. echo can output multiple values and is faster, while
print returns 1, so it can be used in expressions.
8. What is a superglobal in PHP?
○ Superglobals are built-in global arrays in PHP that can be accessed from anywhere in the
script, such as $_POST, $_GET, $_SESSION, $_COOKIE, and $_SERVER.
9. What are PHP constants?
○ Constants are variables whose values cannot change once defined. They are defined
using the define() function.
10. What is the purpose of $_GET and $_POST?
○ $_GET and $_POST are used to collect form data. $_GET retrieves data sent via URL
parameters, while $_POST retrieves data sent through HTTP POST requests.
Control Structures
11. What are control structures in PHP?
○ Control structures control the flow of execution in a script, such as if, else, elseif, switch,
for, while, and foreach loops.
12. How does the if statement work in PHP?
○ The if statement executes a block of code if a specified condition is true.
13. What is a switch statement?
○ The switch statement allows you to test a variable against multiple cases, executing
different code based on the match.
14. What is the difference between while and do-while loops?
○ while checks the condition before executing the loop, while do-while executes the loop
at least once before checking the condition.
15. How does a foreach loop work in PHP?
○ foreach iterates over each element in an array. It’s ideal for working with associative and
indexed arrays.
Functions and Arrays
16. How do you define a function in PHP?
○ Use the function keyword followed by the function name and parentheses (e.g., function
functionName() { ... }).
17. What is the purpose of return in PHP functions?
○ The return statement stops the function execution and sends the result back to the
caller.
18. What are PHP arrays?

core subjects important Page 1


18. What are PHP arrays?
○ Arrays are variables that hold multiple values in one single variable. Types of arrays
include indexed, associative, and multidimensional arrays.
19. What is an associative array in PHP?
○ An associative array uses named keys that you assign to values (e.g., $array = array("key"
=> "value");).
20. How do you merge two arrays in PHP?
○ Use the array_merge() function to combine two arrays.
Object-Oriented PHP
21. What is OOP in PHP?
○ Object-Oriented Programming (OOP) is a programming paradigm in PHP where code is
organized into objects that contain properties (attributes) and methods (functions).
22. How do you create a class in PHP?
○ Use the class keyword followed by the class name (e.g., class MyClass { ... }).
23. What is a constructor in PHP?
○ A constructor is a special function that automatically executes when an object of a class
is created. In PHP, it’s named __construct.
24. What are access modifiers in PHP?
○ Access modifiers define the visibility of class properties and methods: public, private,
and protected.
25. What is inheritance in PHP?
○ Inheritance is an OOP concept where one class (child) can inherit properties and
methods from another class (parent).
26. What is an interface in PHP?
○ An interface defines methods that must be implemented by any class that uses it,
promoting consistency across classes.
27. What is polymorphism in PHP?
○ Polymorphism allows objects to be treated as instances of their parent class, enabling a
single function to handle different types of objects.
28. What is method overloading in PHP?
○ PHP doesn’t support method overloading natively, but you can achieve it with __call()
magic methods for undefined or dynamic methods.
29. What is encapsulation in PHP?
○ Encapsulation restricts direct access to object properties, enforcing controlled access
through getters and setters.
30. What is a trait in PHP?
○ Traits are used to include methods in multiple classes, promoting code reuse without
inheritance.
Error Handling and Exceptions
31. What is error handling in PHP?
○ Error handling involves capturing and managing errors in a PHP script using error-
reporting functions and custom error handlers.
32. What is an exception in PHP?
○ Exceptions are unexpected events that can be thrown and caught to prevent PHP scripts
from crashing unexpectedly.
33. How do you handle exceptions in PHP?
○ Use try, catch, and finally blocks to handle exceptions. Code within try is executed, and if
an exception is thrown, it is caught in catch.
34. What are the different types of errors in PHP?
○ Types include Parse errors (syntax errors), Fatal errors (critical errors), Warning errors
(non-fatal), and Notice errors (minor).
35. What does @ do in PHP?
○ The @ operator suppresses error messages for expressions that generate errors.
Database Operations
36. How do you connect to a MySQL database in PHP?
Use mysqli_connect() or PDO to establish a connection.

core subjects important Page 2


○ Use mysqli_connect() or PDO to establish a connection.
37. What is PDO in PHP?
○ PDO (PHP Data Objects) is a database access layer that supports multiple database types
using the same functions.
38. How do you fetch data from a MySQL database in PHP?
○ Use mysqli_query() and mysqli_fetch_assoc() or PDO's fetch() method.
39. How do you prevent SQL injection in PHP?
○ Use prepared statements with bound parameters using mysqli or PDO to protect against
SQL injection.
40. What is the difference between mysqli_fetch_assoc() and mysqli_fetch_array()?
○ mysqli_fetch_assoc() returns data as an associative array, while mysqli_fetch_array() can
return both numeric and associative arrays.
Sessions and Cookies
41. What is a session in PHP?
○ A session is a way to store information across multiple pages for a user during their visit
to a website. Use session_start() to initiate a session.
42. What is a cookie in PHP?
○ A cookie is a small file stored on the client’s device, used to remember information
across sessions. Use setcookie() to create cookies.
43. How do you delete a session in PHP?
○ Use session_unset() to free all session variables, session_destroy() to destroy the session
data on the server.
44. How do you delete a cookie in PHP?
○ Set the cookie expiration date to a past time using setcookie().
45. What are the differences between sessions and cookies?
○ Sessions are stored on the server, while cookies are stored on the client’s browser.
Sessions expire after a certain period, whereas cookies have configurable expiration
dates.
File Handling
46. How do you open a file in PHP?
○ Use fopen() to open a file, specifying the mode (e.g., r for read, w for write).
47. What is the use of fread() in PHP?
○ fread() reads data from an open file. You specify the file pointer and the number of
bytes to read.
48. How do you write to a file in PHP?
○ Use fwrite() to write data to a file.
49. How do you check if a file exists in PHP?
○ Use file_exists() to check if a file or directory exists.
50. How do you delete a file in PHP?
○ Use unlink() to delete a file.

core subjects important Page 3

You might also like