6. **Associative Array**:
```php
$person = array("name" => "John", "age" => 30, "city" => "New York");
```
This code defines an associative array called `$person` where keys ("name," "age," and "city") are associated with corresponding values ("John," 30, and "New York"). Associative arrays allow you to store key-value pairs.
7. **Loop (foreach)**:
```php
foreach ($fruits as $fruit) {
echo $fruit . "<br>";
}
```
This code uses a `foreach` loop to iterate through the elements of the `$fruits` array and print each fruit followed by a line break. It's commonly used for iterating through arrays.
8. **Function Declaration**:
```php
function greet($name) {
echo "Hello, $name!";
}
```
This code defines a PHP function named `greet` that takes a parameter `$name` and echoes a greeting message using that name. Functions are reusable blocks of code.
9. **Include a File**:
```php
include 'filename.php';
```
This code includes the content of an external PHP file named 'filename.php' into the current PHP script. It's often used for modularizing code and reusing functions or variables.
10. **Form Handling (POST)**:
```php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = $_POST["name"];
// Process the form data
}
```
This code checks if the HTTP request method is POST (typically used for form submissions). If it is, it retrieves the value of the "name" field from the form data. This is a basic example of form data handling in PHP.
These code snippets cover fundamental PHP concepts, including arrays, loops, functions, and form handling. They provide a solid foundation for building more complex PHP applications.
```php
$person = array("name" => "John", "age" => 30, "city" => "New York");
```
This code defines an associative array called `$person` where keys ("name," "age," and "city") are associated with corresponding values ("John," 30, and "New York"). Associative arrays allow you to store key-value pairs.
7. **Loop (foreach)**:
```php
foreach ($fruits as $fruit) {
echo $fruit . "<br>";
}
```
This code uses a `foreach` loop to iterate through the elements of the `$fruits` array and print each fruit followed by a line break. It's commonly used for iterating through arrays.
8. **Function Declaration**:
```php
function greet($name) {
echo "Hello, $name!";
}
```
This code defines a PHP function named `greet` that takes a parameter `$name` and echoes a greeting message using that name. Functions are reusable blocks of code.
9. **Include a File**:
```php
include 'filename.php';
```
This code includes the content of an external PHP file named 'filename.php' into the current PHP script. It's often used for modularizing code and reusing functions or variables.
10. **Form Handling (POST)**:
```php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = $_POST["name"];
// Process the form data
}
```
This code checks if the HTTP request method is POST (typically used for form submissions). If it is, it retrieves the value of the "name" field from the form data. This is a basic example of form data handling in PHP.
These code snippets cover fundamental PHP concepts, including arrays, loops, functions, and form handling. They provide a solid foundation for building more complex PHP applications.