PHP Remaining
PHP Remaining
<?php
$cookie_name = "user";
$expiration = time() + (86400 * 30); // Cookie will expire in 30 days (86400 seconds = 1 day)
setcookie($cookie_name, $cookie_value, $expiration, "/"); // "/" makes the cookie available across
the entire domain
?>
<?php
if (isset($_COOKIE["user"])) {
} else {
?>
20. Write a program in PHP for exception handling for i) divide by zero ii)
checking date format.
i) Divide by zero
<?php
try {
if ($denominator === 0) {
return $result;
} catch (Exception $e) {
return $e->getMessage();
// Example usage:
$numerator = 10;
$denominator = 0;
?>
<?php
function validateDateFormat($dateString) {
try {
if (!$date) {
return $e->getMessage();
}
// Example usage:
$validatedDate = validateDateFormat($dateString);
echo "Validated Date: " . $validatedDate; // Output will be the error message
?>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Validate name
if (empty($_POST["name"])) {
} else {
$name = test_input($_POST["name"]);
// Validate email
if (empty($_POST["email"])) {
} else {
$email = test_input($_POST["email"]);
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
?>
<br><br>
<br><br>
</form>
Output: