PHP NOTES
PHP NOTES
Answer (long):
PHP (Hypertext Preprocessor) is a widely used server-side scripting language designed for web
development but also used as a general-purpose programming language. It can be embedded into
HTML and is commonly used to develop dynamic web pages.
Features:
Server-Side Scripting: PHP is executed on the server, and only the result (HTML) is sent to the client's
browser.
Open Source: PHP is free to use and can be modified according to the developer's needs.
Cross-Platform: PHP can run on various operating systems, such as Windows, Linux, and macOS.
Database Support: PHP works well with many databases, especially MySQL.
Answer (short):
Variables in PHP are used to store data, and they are declared with a dollar sign (`$`) followed by the
variable name. The variable name must start with a letter or an underscore and can contain letters,
numbers, and underscores.
```php
$age = 25;
$name = "John";
```
Answer (long):
An array in PHP is a data structure that can store multiple values in a single variable. PHP supports
both indexed arrays and associative arrays
```php
```php
```
Arrays can store any data type, including integers, strings, and even other arrays.
**Answer (short):**
- `include`: If the file is not found, a warning is issued, and the script continues execution.
- `require`: If the file is not found, a fatal error is generated, and the script stops execution.
**Answer (short):**
Superglobals are built-in global arrays in PHP that are always accessible, regardless of scope.
Common superglobals include:
**Answer (short):**
A function in PHP is a block of code that performs a specific task. It is defined using the `function`
keyword.
```php
function greet($name) {
```
### 7. **What is the difference between `==` and `===` in PHP?**
**Answer (short):**
- `==` is a comparison operator that checks if the values of two variables are equal, without
considering their type.
- `===` is a strict comparison operator that checks both the value and the type of the variables.
**Answer (long):**
- `$_POST`: This superglobal array is used to collect form data sent via HTTP POST method. It is more
secure than `$_GET` because data is sent in the HTTP request body and not in the URL.
```php
$name = $_POST['name'];
```
- `$_GET`: This superglobal array is used to collect form data sent via HTTP GET method. The data is
appended to the URL as query strings, making it less secure.
```php
$name = $_GET['name'];
```
**Answer (short):**
`isset()` checks whether a variable is set and is not `NULL`. It returns `true` if the variable exists and is
not null, otherwise it returns `false`.
```php
$name = "John";
if (isset($name)) {
```
`session_start()` is used to begin a session or resume the current session. It must be called before any
output is sent to the browser. It is commonly used to store user information across different pages.
```php
session_start();
$_SESSION['user'] = "John";
```
### 11. **Explain the difference between `GET` and `POST` methods in PHP.**
**Answer (long):**
- **GET Method:** Data is appended to the URL as query parameters. It has size limitations and is
not secure because the data is visible in the URL.
```php
$name = $_GET['name'];
```
- **POST Method:** Data is sent via HTTP request body. It is more secure than GET because the data
is not visible in the URL, and it has no size limitations.
```php
$name = $_POST['name'];
```
**Answer (long):**
A cookie is a small piece of data sent by the server to the client’s browser, which is stored on the
client's machine and sent back to the server with subsequent requests. Cookies can be used to store
user preferences or session data.
```php
```
### 13. **What is a `foreach` loop in PHP?**
**Answer (short):**
`foreach` is a loop used to iterate over arrays. It is specifically designed to work with arrays and is
simpler than other loops like `for` and `while`.
```php
echo $fruit;
```
**Answer (short):**
An associative array in PHP is an array where each element is accessed using a named key rather
than a numeric index.
```php
```
**Answer (long):**
Errors in PHP can be handled using the `try-catch` block in exception handling. PHP also provides
different types of error reporting:
- **Syntax errors**
- **Runtime errors**
- **Logical errors**
```php
try {
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
```
### 16. **What is the difference between `public`, `private`, and `protected` visibility in PHP?**
**Answer (short):**
- **private:** The property or method can only be accessed within the class.
- **protected:** The property or method can be accessed within the class and by subclasses.
**Answer (long):**
OOP in PHP refers to a programming style that uses objects and classes. The main concepts of OOP in
PHP are:
- **Encapsulation:** Bundling the data and methods that work on the data into a single unit (class).
- **Inheritance:** A class can inherit properties and methods from another class.
- **Polymorphism:** Methods in different classes can have the same name but behave differently.
- **Abstraction:** Hiding complex implementation details and showing only the essential features.
**Answer (short):**
`require_once()` includes and evaluates a file during the execution of the script, but it ensures that
the file is included only once. If the file has already been included, it will not be included again.
**Answer (short):**
The `header()` function is used to send raw HTTP headers to the browser. It is often used for
redirecting users or setting content types.
```php
header("Location: http://example.com");
```
**Answer (short):**
A `constructor` is a special function within a class that is automatically called when an object is
created. It is commonly used to initialize object properties.
```php
class Person {
function __construct($name) {
$this->name = $name
```
**Answer (short):**
```php
unset($name);
```
**Answer (long):**
PHP handles file uploads via the `$_FILES` superglobal array. The user submits a form with the
`enctype="multipart/form-data"` attribute, and PHP processes the file.
```php
if (isset($_FILES['file'])) {
$file_name = $_FILES['file']['name'];
$file_tmp = $_FILES['file']['tmp_name'];
```
**Answer (long):**
PDO (PHP Data Objects) is a database access layer that provides a uniform interface for interacting
with different database management systems (DBMS). It allows developers to write database-
agnostic code and offers several advantages:
- Transaction support.
**Answer (short):**
`include_once()` includes a file, but it ensures that the file is included only once, even if it is called
multiple times in the script.
**Answer (short):**
A trait in PHP is a mechanism for code reuse in single inheritance languages like PHP. It allows you to
include methods from multiple sources into a single class.
```php
trait Logger {
echo $message;
class User {
use Logger;
```