Simple Calculator

<!DOCTYPE html>
<html>
<head>
    <title>PHP Calculator</title>
</head>
<body>

<h2>Simple PHP Calculator</h2>

<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>">
    <label for="num1">Number 1:</label>
    <input type="text" id="num1" name="num1" required><br><br>

    <label for="num2">Number 2:</label>
    <input type="text" id="num2" name="num2" required><br><br>

    <label for="operator">Choose an operator:</label>
    <select id="operator" name="operator">
        <option value="+">Addition (+)</option>
        <option value="-">Subtraction (-)</option>
        <option value="*">Multiplication (*)</option>
        <option value="/">Division (/)</option>
    </select><br><br>

    <input type="submit" value="Calculate">
</form>

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $num1 = $_POST["num1"];
    $num2 = $_POST["num2"];
    $operator = $_POST["operator"];

    switch ($operator) {
        case "+":
            $result = $num1 + $num2;
            break;
        case "-":
            $result = $num1 - $num2;
            break;
        case "*":
            $result = $num1 * $num2;
            break;
        case "/":
            if ($num2 != 0) {
                $result = $num1 / $num2;
            } else {
                $result = "Cannot divide by zero!";
            }
            break;
        default:
            $result = "Invalid operator!";
    }

    echo "<h3>Result: $result</h3>";
}
?>

</body>
</html>

Write to a file

fwrite() function

<?php
$filename = "example.txt";
$file = fopen($filename, "w") or die("Unable to open file!");
$text = "Hello, World!";
fwrite($file, $text);
fclose($file);
echo "Data written to $filename successfully.";
?>

Check if file has been modified

filemtime() function

<?php
$file_path = 'example.txt';

$file_modified_time = filemtime($file_path);
$current_time = time();

if ($file_modified_time > $current_time - 3600) {
    echo "The file has been modified within the last hour.";
} else {
    echo "The file has not been modified within the last hour.";
}
?>

Check if file is utf8

<?php
$file_path = 'example.txt';

$file_content = file_get_contents($file_path, NULL, NULL, 0, 1000);

$encoding = mb_detect_encoding($file_content, 'UTF-8', true);

if ($encoding === 'UTF-8') {
    echo "The file is encoded in UTF-8.";
} else {
    echo "The file is not encoded in UTF-8.";
}
?>

Check if file is pdf

finfo_file() function

<?php
$file_path = 'example.pdf';

$file_info = finfo_open(FILEINFO_MIME_TYPE);
$mime_type = finfo_file($file_info, $file_path);
finfo_close($file_info);

if ($mime_type === 'application/pdf') {
    echo "The file is a PDF.";
} else {
    echo "The file is not a PDF.";
}
?>

Check if file is image

<?php
$file_path = 'example.jpg';

$image_info = getimagesize($file_path);

if ($image_info !== false) {
    if (strpos($image_info['mime'], 'image/') === 0) {
        echo "The file is an image.";
    } else {
        echo "The file is not an image.";
    }
} else {
    echo "The file is not accessible or is not an image.";
}
?>

Check path of file

realpath() function

<?php
$file_path = 'example.txt';

$real_path = realpath($file_path);

if ($real_path !== false) {
    echo "The real path of the file is: $real_path";
} else {
    echo "The file path does not exist or is not accessible.";
}
?>

Check file size

filesize() function

<?php
$filename = 'example.txt';

$filesize = filesize($filename);

if ($filesize !== false) {
    echo "The size of the file is: $filesize bytes";
} else {
    echo "Failed to get the file size.";
}
?>