P- PHP learning Tool practice MCQS
P1. What are the main data types in PHP?
PHP supports scalar types: int, float, string, bool; compound types: array, object; and special types: null, resource. PHP is loosely typed – variables are cast automatically based on context.
---
2. What is the difference between include and require?
· include emits a warning and continues execution if the file is not found.
· require throws a fatal error and stops the script.
Use require for critical dependencies (e.g., configuration), include for optional content.
---
3. Explain PHP superglobals. Name a few.
· $_GET – URL query parameters
· $_POST – form data sent via HTTP POST
· $_SESSION – session data
· $_COOKIE – client-side cookies
· $_SERVER – server and execution environment info
· $_FILES – uploaded files
---
4. How do you connect to a MySQL database securely?
Use PDO or MySQLi with prepared statements to prevent SQL injection. Example with PDO:
```php
$pdo = new PDO('mysql:host=localhost;dbname=test', $user, $pass);
$stmt = $pdo->prepare('SELECT * FROM users WHERE email = ?');
$stmt->execute([$email]);
$user = $stmt->fetch();
```
---
5. What is the difference between == and === in PHP?
· == (loose equality) compares values after type juggling (e.g., 1 == '1' is true).
· === (strict equality) checks both value and type – 1 === '1' is false.
---
6. Explain basic OOP concepts in PHP: class, inheritance, polymorphism.
· Class – blueprint for objects.
· Inheritance – a child class extends a parent using extends, gaining all non‑private properties/methods.
· Polymorphism – methods with the same name can behave differently across classes (achieved via interfaces or abstract classes).
Example:
```php
abstract class Animal {
abstract public function makeSound();
}
class Dog extends Animal {
public function makeSound() { echo "Bark"; }
}
```
---
7. How do you handle exceptions in PHP?
Use try, catch, and optionally finally:
try {
// code that may throw an exception
} catch (Exception $e) {
echo 'Caught: ' . $e->getMessage();
} finally {
// always executed
}
```
You can also create custom exception classes by extending Exception.
---
8. Sessions vs. Cookies – what’s the difference?
· Sessions store data on the server; a session ID is stored in a cookie (or URL). Data is temporary and cleared when the session ends.
· Cookies store data on the client side (browser) and have expiration dates. They are limited to ~4KB and are less secure.
---
9. How can you prevent XSS (Cross‑Site Scripting) attacks?
Always escape output. Use htmlspecialchars($string, ENT_QUOTES, 'UTF-8') when rendering user‑supplied data in HTML. For input, validate and sanitize, but never trust user input.
---
10. What are PHP magic methods? Give two examples.
Magic methods start with __ and are called automatically.
· __construct() – invoked when an object is instantiated.
· __toString() – defines how an object behaves when treated as a string.
Others: __get(), __set(), __call(), __destruct(), etc.
--- PHP learning Tool practice MCQS
These questions cover the fundamentals, security, and modern practices – a solid foundation for any PHP developer. Good luck!
Comments
Post a Comment
Thanks for sharing your thoughts! Stay tuned for more updates