PHP V Ques
PHP V Ques
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?