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

PHP Understanding Test

Uploaded by

stvnsbr2265
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)
5 views3 pages

PHP Understanding Test

Uploaded by

stvnsbr2265
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

PHP Understanding Test

**Section 1: Basics (5 Questions)**


1. What will be the output of the following code?
```php
$x = "5" + 10;
echo gettype($x);
```

2. What is the difference between `==` and `===` in PHP?

3. Explain the role of `$GLOBALS`, `$_SERVER`, and `$_POST` in PHP.

4. Write a PHP script that defines a variable and checks if it is an integer.

5. Fix the syntax error in the following PHP code:


```php
echo "Hello World"
?>
```

**Section 2: Control Structures (5 Questions)**


6. Write a PHP program that prints numbers from 1 to 10 using a `for` loop.

7. What will be the output of the following code?


```php
$num = 10;
if ($num > 5 && $num < 15) {
echo "Pass";
} else {
echo "Fail";
}
```

8. Explain the difference between `break` and `continue` in loops.

9. Rewrite the following `if` condition using a ternary operator:


```php
if ($age >= 18) {
$status = "Adult";
} else {
$status = "Minor";
}
```

10. Write a `switch` case statement to print the name of a day based on the number (1 for Monday,
2 for Tuesday, etc.).

**Section 3: Functions & Arrays (5 Questions)**


11. Write a function in PHP that takes two numbers as parameters and returns their sum.

12. What is the difference between indexed and associative arrays? Provide an example.

13. How do you remove an element from an array in PHP? Write an example.

14. Write a PHP script that finds the largest number in an array.

15. What will be the output of the following code?


```php
$arr = ["Apple", "Banana", "Cherry"];
echo count($arr);
```

**Section 4: Form Handling & Security (5 Questions)**


16. Write a PHP script to retrieve and display form data using `$_POST`.

17. Why should you use `htmlspecialchars()` when handling user input?

18. How do you prevent SQL injection in PHP? Provide an example.

19. What is the difference between `$_SESSION` and `$_COOKIE`?

20. Write a PHP script to start a session and store a username in it.

**Section 5: Database & OOP (5 Questions)**


21. Write a PHP script to connect to a MySQL database.

22. What is a prepared statement, and why is it used?

23. Write a PHP class `Car` with a property `brand` and a method `getBrand()`.

24. What is inheritance in PHP OOP? Provide an example.

25. Write a PHP script to fetch all records from a MySQL table named `users` and display them in a
table.

**Instructions:**
- Write your answers in PHP code format and submit them here.
- Attempt all questions to test your understanding.
- Use comments where necessary to explain your answers.

You might also like