🔒 ponchos blog.

PHP Fuctionalities

Let's explore some more PHP examples to demonstrate different functionalities:

  1. Variables and Data Types:
<?php
// Variables and Data Types
$name = "John Doe";
$age = 30;
$height = 6.2;
$is_student = true;

// Printing variables
echo "Name: " . $name . "<br>";
echo "Age: " . $age . "<br>";
echo "Height: " . $height . "<br>";
echo "Is Student? " . ($is_student ? "Yes" : "No") . "<br>";
?>
  1. Conditional Statements:
<?php
// Conditional Statements
$score = 85;

if ($score >= 90) {
    echo "Grade: A";
} elseif ($score >= 80) {
    echo "Grade: B";
} elseif ($score >= 70) {
    echo "Grade: C";
} else {
    echo "Grade: F";
}
?>
  1. Loop (For loop):
<?php
// For loop
for ($i = 1; $i <= 5; $i++) {
    echo "Iteration: " . $i . "<br>";
}
?>
  1. Arrays:
<?php
// Arrays
$fruits = array("Apple", "Banana", "Orange", "Mango");

// Accessing array elements
echo "Fruit at index 0: " . $fruits[0] . "<br>";

// Looping through the array
echo "Fruits: ";
foreach ($fruits as $fruit) {
    echo $fruit . ", ";
}
?>
  1. Functions:
<?php
// Functions
function addNumbers($a, $b) {
    return $a + $b;
}

// Calling the function
$result = addNumbers(5, 3);
echo "Result: " . $result;
?>
  1. Form handling (POST method):
<!DOCTYPE html>
<html>
<head>
    <title>Form Example</title>
</head>
<body>
    <form method="POST">
        <label for="name">Name:</label>
        <input type="text" name="name" id="name">
        <br>
        <label for="email">Email:</label>
        <input type="email" name="email" id="email">
        <br>
        <input type="submit" name="submit" value="Submit">
    </form>

    <?php
    // Handling form submission
    if ($_SERVER["REQUEST_METHOD"] == "POST") {
        $name = $_POST["name"];
        $email = $_POST["email"];

        echo "Submitted Name: " . $name . "<br>";
        echo "Submitted Email: " . $email . "<br>";
    }
    ?>
</body>
</html>

These examples showcase different PHP features, such as variables, conditional statements, loops, arrays, functions, and handling form submissions. Feel free to experiment with them and explore more PHP functionalities to enhance your web development skills.