16. **Regular Expressions**:
```php
$pattern = "/world/i";
$text = "Hello, World!";
if (preg_match($pattern, $text)) {
echo "Match found!";
}
```
This code uses regular expressions to check if the string `$text` contains the pattern `/world/i`, where the `i` flag makes the pattern case-insensitive. If a match is found, it prints "Match found!".
17. **Redirect to Another Page**:
```php
header("Location: newpage.php");
exit();
```
This code redirects the user to another page named "newpage.php" using the `header` function. The `exit()` function is called to ensure that no further code is executed after the redirection.
18. **Error Handling**:
```php
try {
// Code that may throw an exception
} catch (Exception $e) {
echo "An error occurred: " . $e->getMessage();
}
```
This code demonstrates error handling using a try-catch block. If an exception is thrown within the try block, it is caught in the catch block, and an error message is displayed.
19. **Database Connection (MySQL)**:
```php
$conn = mysqli_connect("localhost", "username", "password", "database_name");
```
This code establishes a connection to a MySQL database on the localhost server using the `mysqli_connect` function. You need to replace "username," "password," and "database_name" with your actual database credentials.
20. **Session Handling**:
```php
session_start();
$_SESSION["user_id"] = 123;
```
This code starts a PHP session using `session_start()` and sets a session variable `$_SESSION["user_id"]` to the value 123. Session variables are used to store user-specific data across multiple requests.
These code snippets cover a variety of PHP features, including regular expressions, header redirection, error handling, database connections, and session management. They are essential for building dynamic and interactive web applications in PHP.
```php
$pattern = "/world/i";
$text = "Hello, World!";
if (preg_match($pattern, $text)) {
echo "Match found!";
}
```
This code uses regular expressions to check if the string `$text` contains the pattern `/world/i`, where the `i` flag makes the pattern case-insensitive. If a match is found, it prints "Match found!".
17. **Redirect to Another Page**:
```php
header("Location: newpage.php");
exit();
```
This code redirects the user to another page named "newpage.php" using the `header` function. The `exit()` function is called to ensure that no further code is executed after the redirection.
18. **Error Handling**:
```php
try {
// Code that may throw an exception
} catch (Exception $e) {
echo "An error occurred: " . $e->getMessage();
}
```
This code demonstrates error handling using a try-catch block. If an exception is thrown within the try block, it is caught in the catch block, and an error message is displayed.
19. **Database Connection (MySQL)**:
```php
$conn = mysqli_connect("localhost", "username", "password", "database_name");
```
This code establishes a connection to a MySQL database on the localhost server using the `mysqli_connect` function. You need to replace "username," "password," and "database_name" with your actual database credentials.
20. **Session Handling**:
```php
session_start();
$_SESSION["user_id"] = 123;
```
This code starts a PHP session using `session_start()` and sets a session variable `$_SESSION["user_id"]` to the value 123. Session variables are used to store user-specific data across multiple requests.
These code snippets cover a variety of PHP features, including regular expressions, header redirection, error handling, database connections, and session management. They are essential for building dynamic and interactive web applications in PHP.