Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

PHP NOTES

PHP (Hypertext Preprocessor) is a server-side scripting language used for web development, featuring server-side execution, open-source availability, and cross-platform compatibility. Key concepts include variables, arrays, functions, superglobals, and object-oriented programming principles. PHP also supports file uploads, error handling, and provides mechanisms like PDO for secure database interactions.

Uploaded by

wacafo2506
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

PHP NOTES

PHP (Hypertext Preprocessor) is a server-side scripting language used for web development, featuring server-side execution, open-source availability, and cross-platform compatibility. Key concepts include variables, arrays, functions, superglobals, and object-oriented programming principles. PHP also supports file uploads, error handling, and provides mechanisms like PDO for secure database interactions.

Uploaded by

wacafo2506
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

PHP NOTES

1. What is PHP? Explain its features.

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.

Embedded in HTML: PHP can be written directly within HTML code.

Supports Object-Oriented Programming (OOP).

2.What are variables in PHP?

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";

```

3. Explain the concept of an array in PHP.

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

Indexed Array: Each element is stored with a numeric index.

```php

$fruits = array("Apple", "Banana", "Cherry");


```

- **Associative Array:** Each element is stored with a key-value pair.

```php

$person = array("name" => "John", "age" => 25);

```

Arrays can store any data type, including integers, strings, and even other arrays.

### 4. **What is the difference between `include` and `require` in PHP?**

**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.

### 5. **Explain PHP Superglobals.**

**Answer (short):**

Superglobals are built-in global arrays in PHP that are always accessible, regardless of scope.
Common superglobals include:

- `$_GET` (for data sent via URL)

- `$_POST` (for data sent via form submission)

- `$_SESSION` (for session variables)

- `$_COOKIE` (for cookie values)

- `$_REQUEST` (for data sent via GET, POST, or COOKIE)

- `$_FILES` (for file uploads)

6. **What is a function in PHP? How do you define and call one?**

**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) {

return "Hello, " . $name;

echo greet("John"); // Output: Hello, John

```
### 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.

### 8. **Explain `$_POST` and `$_GET` in PHP.**

**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'];

```

### 9. **What is the use of `isset()` function in PHP?**

**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)) {

echo "Name is set.";

```

### 10. **What is the purpose of the `session_start()` function in PHP?**


**Answer (short):**

`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'];

```

### 12. **What is a cookie in PHP?**

**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

setcookie("user", "John", time() + 3600); // expires in 1 hour

```
### 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

$fruits = array("Apple", "Banana", "Cherry");

foreach ($fruits as $fruit) {

echo $fruit;

```

### 14. **What is a PHP associative array?**

**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

$person = array("name" => "John", "age" => 25);

```

### 15. **How do you handle errors in 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 = new PDO("mysql:host=localhost;dbname=myDB", "root", "");

$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

} catch (PDOException $e) {

echo "Connection failed: " . $e->getMessage();


}

```

### 16. **What is the difference between `public`, `private`, and `protected` visibility in PHP?**

**Answer (short):**

- **public:** The property or method is accessible from anywhere.

- **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.

### 17. **What is object-oriented programming (OOP) in PHP?**

**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.

### 18. **Explain the `require_once()` function in PHP.**

**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.

### 19. **What is `header()` function used for in PHP?**

**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");

```

### 20. **Explain the concept of a `constructor` in PHP.**

**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

```

### 21. **What is the purpose of `unset()` in PHP?**

**Answer (short):**

The `unset()` function is used to destroy a variable, making it no longer available.

```php

unset($name);

```

### 22. **How does PHP handle file uploads?**

**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'];

move_uploaded_file($file_tmp, "uploads/" . $file_name);

```

### 23. **What are `PDO` and its advantages?**

**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:

- Support for multiple databases (e.g., MySQL, PostgreSQL, SQLite).

- Prepared statements for better security (prevents SQL injection).

- Transaction support.

### 24. **What is `include_once()`?**

**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.

### 25. **What is a `trait` in PHP?**

**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 {

public function log($message) {

echo $message;

class User {

use Logger;

```

You might also like