Wipunit 4 Program
Wipunit 4 Program
<html>
<head>
<h1>Simple Calculator</h1><br>
</head>
<body>
<form>
First Number:<input name="n1" value=""><br>
Second Number:<input name="n2" value=""><br>
<input type="submit" name="sub" value="+">
<input type="submit" name="sub" value="-">
<input type="submit" name="sub" value="x">
<input type="submit" name="sub" value="/"><br>
<br>Result: <input type='text' value="<?php echo $ans; ?>"><br>
</form>
<?php
if(isset($_POST['sub'])){
$num1=$_POST['n1'];
$num2=$_POST['n2'];
$oprnd=$_POST['sub'];
if($oprnd=="+")
$ans=$num1+$num2;
else if($oprnd=="-")
$ans=$num1-$num2;
else if($oprnd=="x")
$ans=$num1*$num2;
else if($oprnd=="/")
$ans=$num1/$num2;
}?>
Output:
3)Write a PHP program that tests whether an email address is input correctly. Test your
program with both valid and invalid email addresses.
Create a function validateEmail() , Use the filter_var() function that takes $email as first
parameter and FILTER_VALIDATE_EMAIL as the second parameter.
Apply the if else condition on the filter_var() function. In the if block, display the message
saying the email is valid, and in the else condition, display that email is invalid. Outside the
function, call the function two times.
Assume that the email address provided in the example is accessed from a form using
the$_POST variable.
<?php
function validateEmail($email) {
if(filter_var($email, FILTER_VALIDATE_EMAIL)) {
else {
validateEmail('[email protected]');
validateEmail('[email protected]');
?>
<?php
Output:
(b)use preg_match();
preg_match() function takes two parameters where the first one is the regular expression, and
the second one is the email to be checked
# php 7.x
<?php
$email_first = '[email protected]';
$email_second ='firstlast@11gmail,com';
function validateEmail($email) {
$regex = "/^([a-zA-Z0-9\.]+@+[a-zA-Z]+(\.)+[a-zA-Z]{2,3})$/";
echo preg_match($regex, $email) ? "The email is valid"."<br>" :"The email is not
valid";
}
validateEmail($email_first);
validateEmail($email_second);
?>
Output:
Example:
<html>
<head>
<title>Sending HTML email using PHP</title>
</head>
<body>
<?php
$to = "[email protected]";
$subject = "This is subject";
</body>
</html>