0% found this document useful (0 votes)
6 views5 pages

Wipunit 4 Program

The document provides PHP code examples for creating a simple calculator, validating email addresses using filter_var and regex, and sending emails using the mail() function. It includes HTML forms for user input and demonstrates how to handle form submissions and display results. The document also explains the requirements for valid email formats and the parameters needed for sending emails in PHP.

Uploaded by

priya j
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views5 pages

Wipunit 4 Program

The document provides PHP code examples for creating a simple calculator, validating email addresses using filter_var and regex, and sending emails using the mail() function. It includes HTML forms for user input and demonstrates how to handle form submissions and display results. The document also explains the requirements for valid email formats and the parameters needed for sending emails in PHP.

Uploaded by

priya j
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

1) Design simple calculator using PHP

<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)) {

echo "{$email}: A valid email"."<br>";

else {

echo "{$email}: Not a valid email"."<br>";

validateEmail('[email protected]');
validateEmail('[email protected]');

?>

<?php

Output:

[email protected]: A valid email

[email protected]:Not a valid email

(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:

The email is valid

The email is not valid


A valid email contains a recipient name,@ symbol, a domain, and a top-level domain. The
above-created regex expression accepts the recipient name as alphanumeric values. The
alphabet consists of both the uppercase and the lowercase. It also accepts a period. The email
must have @ symbol. The domain contains the alphabets only. The email should have a
period then. The top-level domain should only consist of the alphabets and should have a
length of two or three. The regex expression is created based on this rule.
The first email is valid since it satisfies all the rules, but the second email is invalid. It is
invalid because there is a number in the domain name, and there is no period before the top-
level domain.
4)Design application to send an email using PHP.
PHP makes use of mail() function to send an email. This function requires three mandatory
arguments that specify the recipient's email address, the subject of the the message and the
actual message additionally there are other two optional parameters.

mail( to, subject, message, headers, parameters );

S.No Parameter & Description


1 To
Required. Specifies the receiver / receivers of the email
2 Subject
Required. Specifies the subject of the email. This parameter cannot
contain any newline characters
3 Message
Required. Defines the message to be sent. Each line should be
separated with a LF (\n). Lines should not exceed 70 characters
4 Headers
Optional. Specifies additional headers, like From, Cc, and Bcc. The
additional headers should be separated with a CRLF (\r\n)
5 Parameters
Optional. Specifies an additional parameter to the send mail program

Example:

<html>

<head>
<title>Sending HTML email using PHP</title>
</head>
<body>

<?php
$to = "[email protected]";
$subject = "This is subject";

$message = "<b>This is HTML message.</b>";


$message .= "<h1>This is headline.</h1>";

$header = "From:[email protected] \r\n";


$header .= "Cc:[email protected] \r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-type: text/html\r\n";

$retval = mail ($to,$subject,$message,$header);

if( $retval == true ) {


echo "Message sent successfully...";
}else {
echo "Message could not be sent...";
}
?>

</body>
</html>

You might also like