0% found this document useful (0 votes)
23 views49 pages

Record PHP

The document contains examples of PHP code demonstrating various arithmetic, comparison, logical, and string operators and functions. It includes examples calculating arithmetic operations on user-submitted numbers, comparing values, evaluating logical expressions, and manipulating strings. Additional examples show generating a Fibonacci series, calculating student grades, finding the maximum of three numbers, and using date, math, and array functions. The document serves as a reference of different PHP operators and functions.

Uploaded by

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

Record PHP

The document contains examples of PHP code demonstrating various arithmetic, comparison, logical, and string operators and functions. It includes examples calculating arithmetic operations on user-submitted numbers, comparing values, evaluating logical expressions, and manipulating strings. Additional examples show generating a Fibonacci series, calculating student grades, finding the maximum of three numbers, and using date, math, and array functions. The document serves as a reference of different PHP operators and functions.

Uploaded by

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

1 a) ARITHMETIC OPERATORS

PROGRAM:

<html>
<body style="background-color:#ffcccc;">
<form method="post"><br><br><br><br><br><br><br><br><br><br>
<center>
<h2> ARITHMETIC OPERATION</h2>
Enter the first number:
<input type="number" name="number1"/><br><br>
Enter the second number:
<input type="number" name="number2"/><br><br>
<input type="submit" name="submit" value="Add">
<input type="submit" name="submit1" value="Sub">
<input type="submit" name="submit2" value="Mul">
<input type="submit" name="submit3" value="Div">
</form>
</center>
<center>
<?php
if(isset($_POST['submit']))
{
$number1=$_POST['number1'];
$number2=$_POST['number2'];
$sum=$number1+$number2;
echo "The sum of $number1 and $number2 is: ".$sum;
}
if(isset($_POST['submit1']))
{
$number1=$_POST['number1'];
$number2=$_POST['number2'];
$sum=$number1-$number2;
echo "The subtraction of $number1 and $number2 is: ".$sum;
}
if(isset($_POST['submit2']))
{
$number1=$_POST['number1'];
$number2=$_POST['number2'];
$sum=$number1*$number2;
echo "The multiplication of $number1 and $number2 is: ".$sum;
}
if(isset($_POST['submit3']))
{
$number1=$_POST['number1'];
$number2=$_POST['number2'];
$sum=$number1/$number2;
echo "The division of $number1 and $number2 is: ".$sum;
}
?>
</center>
</body>
</html>
OUTPUT:
1.b) COMPARSION OPERATOR
PROGRAM:
<html>
<body style="background-color:#fdacac;">
<form method="post"><br><br><br><br><br><br><br><br><br><br>
<center>
<h2> COMPARISON OPERATION</h2>
First Number:
<input type="number" name="num1"/><br><br>
Second Number:
<input type="number" name="num2"/><br><br>
<input type="submit" name="submit1" value="Equal to">
<input type="submit" name="submit2" value="Not Equal to">
<input type="submit" name="submit3" value="Greater than">
<input type="submit" name="submit4" value="Smaller than">
</form>
<?php
if(isset($_POST['submit1']))
{
$num1=$_POST['num1'];
$num2=$_POST['num2'];
if($num1==$num2)
{
echo "$num1 and $num2 is equal";
}
else
{
echo "$num1 and $num2 is not equal";
}
}
if(isset($_POST['submit2']))
{
$num1=$_POST['num1'];
$num2=$_POST['num2'];
if($num1!=$num2)
{
echo "$num1 and $num2 is not equal";
}
else
{
echo "$num1 and $num2 is equal";
}
}

if(isset($_POST['submit3']))
{
$num1=$_POST['num1'];
$num2=$_POST['num2'];
if($num1>$num2)
{
echo "$num1 is greater than $num2";
}
else
{
echo "$num1 is not greater than $num2 or it is equal to $num2";
}
}
if(isset($_POST['submit4']))
{
$num1=$_POST['num1'];
$num2=$_POST['num2'];
if($num1!=$num2)
{
if($num1<$num2)
{
echo "$num1 is smaller than $num2";
}
else
{
echo "$num1 is not smaller than $num2 or it is equal to $num2";
}
}
}
?>
</center>
</body>
</html>
OUTPUT:
1.c) LOGICAL OPERATOR
PROGRAM:

<html>
<body>
<center>
<h1>LOGICAL OPERATION</h1><br><br>
<body style="background-color:#e2ccff;">
<form method="post">
Enter a Number:
<input type="number" name="number"/><br><br>
Enter a Number1:
<input type="number" name="number1"/><br><br>
<input type="submit" name="submit" value="AND">
<input type="submit" name="submit1" value="OR">
<input type="submit" name="submit2" value="XOR">
<input type="submit" name="submit3" value="NOT">
</center>
</form>
<center>
<?php
if(isset($_POST['submit']))
{
$number=$_POST['number'];
$number1=$_POST['number1'];
if(($number==$number1)&&($number>10))
{
echo "AND is true";
}
else
{
echo "AND is false";
}
}
if(isset($_POST['submit1']))
{
$number=$_POST['number'];
$number1=$_POST['number1'];
if(($number==$number1)||($number>10))
{
echo "OR is true";
}
else
{
echo "OR is false";
}
}
if(isset($_POST['submit2']))
{
$number=$_POST['number'];
$number1=$_POST['number1'];
if(($number==$number1) xor ($number>10))
{
echo "XOR is true";
}
else
{
echo "XOR is false";
}
}
if(isset($_POST['submit3']))
{
$number=$_POST['number'];
$number1=$_POST['number1'];
if(!($number>10))
{
echo "NOT is true";
}
else
{
echo "NOT is false";
}
}
?>
</center>
</body>
</html>
OUTPUT:
2. FIBONACCI SERIES

PROGRAM:
<html>
<body>
<h2>Fibonacci Series</h2>
<form action="" method="post">
Enter the number :
<input type="text" name="number" />
<input type="submit" />
</form>
</body>
</html>
<?php
if ($_POST)
{
$n1 = 0;
$n2 = 1;
$n = $_POST['number'];
while ($n1 <= $n) {
echo $n1 . "</br>";
$n3 = $n1 + $n2;
$n1 = $n2;
$n2 = $n3;
}
}
?>
OUTPUT:
3.GRADE FOR STUDENT MARKS

PROGRAM:
<html>
<body>
<form method = "POST">
Enter English Mark:
<input type="number"name="sub1"/><br><br>
Enter Tamil Mark:
<input type="number"name="sub2"/><br><br>
Enter Maths Mark:
<input type="number"name="sub3"/><br><br>
Enter Science Mark:
<input type="number"name="sub4"/><br><br>
Enter Social Mark:
<input type="number"name="sub5"/><br><br>
<input type="submit"name="submit"value="Result"/></br>
<?php
if(isset($_POST['submit']))
{
$sub1=$_POST['sub1'];
$sub2=$_POST['sub2'];
$sub3=$_POST['sub3'];
$sub4=$_POST['sub4'];
$sub5=$_POST['sub5'];
// It will calculate total, average, percentage, and grade
$total = $sub1 + $sub2 + $sub3 + $sub4 + $sub5;
$average = $total / 5.0;
$percentage = ($total / 500.0) * 100;
if ($average >= 90)
$grade = "A";
else {
if ($average >= 80 && $average < 90)
$grade = "B";
else {
if ($average >= 70 && $average < 80)
$grade = "C";
else {
if ($average >= 60 && $average < 70)
$grade = "D";
else
$grade = "E";
}
}
}

// It will print the final output


echo "The Total marks = " . $total . "/500<br>";
echo "The Average marks = " . $average . "<br>";
echo "The Percentage = " . $percentage . "%<br>";
echo "The Grade = '" . $grade . "'<br>";
}
?>
OUTPUT:
4. MAXIMUM OF THREE NUMBERS

PROGRAM:

<html>
<body>
<form method = "POST">
Enter number1:
<input type="number"name="num1"/><br><br>
Enter number2:
<input type="number"name="num2"/><br><br>
Enter number3:
<input type="number"name="num3"/><br><br>

<input type="submit"name="submit"value="Result"/></br>
<?php
if(isset($_POST['submit']))
{
$num1=$_POST['num1'];
$num2=$_POST['num2'];
$num3=$_POST['num3'];
if($num1>$num2 && $num1>$num3)
{
echo "The max number is:".$num1;
}
else
{
if($num2>$num1 && $num2>$num3)
{
echo "The max number is:".$num2;
}
else
echo "The max number is:".$num3;
}
}
?>
OUTPUT:
5. STRING FUNCTIONS

PROGRAM:

<html>
<body>
<form method = "POST">
Enter String1:
<input type="text"name="num1"/><br><br>
Enter String2:
<input type="text"name="num2"/><br><br>
<input type="submit"name="submit"value="Strlen"/></br></br>
<input type="submit"name="submit1"value="Strcmp"/></br></br>
<input type="submit"name="submit2"value="Strrev"/></br></br>
<input type="submit"name="submit3"value="Str to upper"/></br></br>
<input type="submit"name="submit4"value="Str to lower"/></br></br>
<?php
if(isset($_POST['submit']))
{
$num1=$_POST['num1'];
$num2=$_POST['num2'];
if($num1)
{
echo "The length of string is:".strlen($num1);
}
else if($num2)
{
echo "The length of string is:".strlen($num2);
}
else
{
echo "Enter any String...!";
}
}
if(isset($_POST['submit1']))
{
$num1=$_POST['num1'];
$num2=$_POST['num2'];
echo "The comparision of string is:".strcmp($num1,$num2);
}
if(isset($_POST['submit2']))
{
$num1=$_POST['num1'];
$num2=$_POST['num2'];
if($num1)
{
echo "The reverse of string is:".strrev($num1);
}
else if($num2)
{
echo "The reverse of string is:".strrev($num2);
}
else
{
echo "Enter any String...!";
}
}
if(isset($_POST['submit3']))
{
$num1=$_POST['num1'];
$num2=$_POST['num2'];
if($num1)
{
echo "The Uppercase of string is:".strtoupper($num1);
}
else if($num2)
{
echo "The Uppercase of string is:".strtoupper($num2);
}
else
{
echo "Enter any String...!";
}
}
if(isset($_POST['submit4']))
{
$num1=$_POST['num1'];
$num2=$_POST['num2'];
if($num1)
{
echo "The Lowercase of string is:".strtolower($num1);
}
else if($num2)
{
echo "The Lowercase of string is:".strtolower($num2);
}
else
{
echo "Enter any string...!";
}
}
?>
OUTPUT:
6. DATE FUNCTIONS

PROGRAM:

<?php
$today = date("d/m/Y");
echo $today;
echo "<br><br>";
$timestamp = time();
echo($timestamp);
echo(date("F d, Y h:i:s", $timestamp));
echo "<br><br>";
echo date('l jS \of F Y h:i:s A');
echo "<br><br>";
echo date("l");
echo "<br>";
?>
OUTPUT:
7. MATH FUNCTIONS

PROGRAM:

<html>
<body>
<form method = "POST">
Enter Number1:
<input type="text"name="num1"/><br><br>
Enter Number2:
<input type="text"name="num2"/><br><br>
<input type="submit"name="submit"value="abs()"/></br></br>
<input type="submit"name="submit1"value="asin"/></br></br>
<input type="submit"name="submit2"value="acos"/></br></br>
<input type="submit"name="submit3"value="atan"/></br></br>
<input type="submit"name="submit4"value="max"/></br></br>
<input type="submit"name="submit5"value="min"/></br></br>
<?php
if(isset($_POST['submit']))
{
$num1=$_POST['num1'];
echo "The absolute Value is:".abs($num1);
}
if(isset($_POST['submit1']))
{
$num1=$_POST['num1'];
echo "The absolute Value is:".asin($num1);
}
if(isset($_POST['submit2']))
{
$num1=$_POST['num1'];
echo "The absolute Value is:".acos($num1);
}
if(isset($_POST['submit3']))
{
$num1=$_POST['num1'];
echo "The absolute Value is:".atan($num1);
}
if(isset($_POST['submit4']))
{
$num1=$_POST['num1'];
$num2=$_POST['num2'];
if($num1>$num2)
{
echo "The maximum number is:".$num1;
}
else
{
echo "The maximum number is:".$num2;
}
}
if(isset($_POST['submit5']))
{
$num1=$_POST['num1'];
$num2=$_POST['num2'];
if($num1<$num2)
{
echo "The minimum number is:".$num1;
}
else
{
echo "The minimum number is:".$num2;
}
}
?>
</body>
</html>
OUTPUT:
8. ARRAY FUNCTION

PROGRAM:

<!DOCTYPE html>
<html>
<body>
<?php
echo "<b>SIMPLE ARRAY FUNCTION </br></br></b>";
$size = array
(
array("Pramod",45,5),
array("Ben",65,4),
array("Santosh",50,6),
);
echo $size[0][0].": Weight: ".$size[0][1].", Height: ".$size[0][2].".<br></br>";
echo "<b>ARRAY USING CHANGE KEY CASE
FUNCTION</br></br></b>";
$weight=array("Rahul"=>"50","Vibha"=>"45","Santa"=>"80");
print_r (array_change_key_case($weight,CASE_UPPER));
echo "</br></br>";
echo "<b>SORTING NUMBER IN ARRAY USING SORT FUNCTION
</br></br></b>";
$numbers=array("100","160","20","67");
sort($numbers);
foreach( $numbers as $n )
{
echo "$n<br />";
}
echo "</br></br>";
echo "<b>ARRAY USING COUNT FUNCTION </br></br></b>";
$flowers=array("lily","lotus","rose","Marigold");
echo count($flowers);
?>
</body>
</html>
OUTPUT:
9. STUDENT REGISTRATION FORM

PROGRAM:
FORM.HTML
<html>
<head>
<title>Registration</title>
<style>
.main
{
width:30%;
margin:2em auto;
border:2px solid black;
padding:1em;
}
.main input[type="submit"]
{
width:95%;
border:1px solid black;
padding:.5em;
margin:.5em;
}
.main input[type="password"]
{
width:95%;
border:1px solid black;
padding:.5em;
margin:.5em;
}
.main input[type="text"],.main input[type="email"]
{
width:45%;
border:1px solid black;
padding:.5em;
margin:.5em;
}
</style>
</head>
<body>
<div class="main">
<form method="post" action="result.php">
<center><h2> Student Registration </h2></center>
<input type="text" name="fname" placeholder="First Name">
<input type="text" name="lname" placeholder="Last Name">
<input type="email" name="email" placeholder="Email">
<input type="text" name="mobile" placeholder="Mobile">
<center><input type="text" name="city" placeholder="City"> <br>
<label for="start"> Date of birth:</label>
<input type="date" id="start" name="trip-start" value="">
<br>
</center>
<hr>
Gender<br>
<input type="radio" name="gender" value="Male">Male<br>
<input type="radio" name="gender" value="female">Female<br>
<hr>
Hobbies<br>
<input type="checkbox" name="hobby[]" value="Cricket">Cricket<br>
<input type="checkbox" name="hobby[]" value="Football">Football<br>
<input type="checkbox" name="hobby[ ]" value="Chess">Chess<br>
<hr>
<input type="Password" name="pass" placeholder="Password"><br>
<input type="submit" name="submit" value="Register">
</form>
</div>
</body>
</html>

RESULT.PHP

<?php
echo "<b>YOUR FORM HAS BEEN SUBMITTED
SUCCESSFULLY...!!</b></br></br>";
if(isset($_POST['submit']))
{
$fn=$_POST['fname'];
$ln=$_POST['lname'];
$em=$_POST['email'];
$mob=$_POST['mobile'];
$city=$_POST['city'];
$gender=$_POST['gender'];
$hobies=$_POST['hobby'];
$pass=$_POST['pass'];
echo "First name : $fn <br><br>";
echo "Last name : $ln <br><br>";
echo "Email : $em <br><br>";
echo "Mobile : $mob <br><br>";
echo "City : $city <br><br>";
echo "Gender : $gender <br><br>";
echo "<h3>Hobbies</h3>";
$i=0;
while($i<sizeof($hobies))
{
echo $hobies[$i]."<br>";
$i++;
}
}
?>
OUTPUT:
WEBSITE REGISTRATION FORM
PROGRAM:

WEBSITE.HTML

<!DOCTYPE html>
<html>
<head>
<title>Registration Form</title>
</head>
<body>
<form action="process.php" method="POST">
<label for="name">Name:</label><br>
<input type="text" id="name" name="name"><br>
<label for="email">Email:</label><br>
<input type="text" id="email" name="email"><br>
<label for="gender">Gender:</label><br>
<input type="radio" id="male" name="gender" value="male">
<label for="male">Male</label><br>
<input type="radio" id="female" name="gender" value="female">
<label for="female">Female</label><br>
<label for="interests">Interests:</label><br>
<input type="checkbox" id="sports" name="interests[]" value="sports">
<label for="sports">Sports</label><br>
<input type="checkbox" id="music" name="interests[]" value="music">
<label for="music">Music</label><br>
<input type="checkbox" id="travel" name="interests[]" value="travel">
<label for="travel">Travel</label><br>
<label for="country">Country:</label><br>
<select id="country" name="country">
<option value="india">India</option>
<option value="usa">USA</option>
<option value="uk">UK</option>
<option value="canada">Canada</option>
</select><br>
<input type="submit" value="Submit">
</form>
</body>
</html>

PROCESS.PHP

<!DOCTYPE html>
<html>
<head>
<title>Registration Form - Processed Data</title>
</head>
<body>
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$gender = $_POST['gender'];
$interests = implode(", ", $_POST['interests']);
$country = $_POST['country'];
echo "<p><strong>Name:</strong> $name</p>";
echo "<p><strong>Email:</strong> $email</p>";
echo "<p><strong>Gender:</strong> $gender</p>";
echo "<p><strong>Interests:</strong> $interests</p>";
echo "<p><strong>Country:</strong> $country</p>";
?>
</body>
</html>
OUTPUT:
11.PHP SCRIPT TO CONNECT MYSQL

PROGRAM:

<html>
<head>
<title>Registration</title>
<style>
.main
{
width:30%;
margin:2em auto;
border:2px solid black;
padding:1em;
}
.main input[type="submit"]
{
width:95%;
border:1px solid black;
padding:.5em;
margin:.5em;
}
</style>
</head>
<body style="background-color:powderblue;">
<div class="main">
<form method="post" action="output.php">
<center><h2> Student Registration </h2></center>
<input type="text" name="id" placeholder="ID">
<input type="text" name="sname" placeholder="Student Name">
<input type="text" name="sdept" placeholder="Department">
<input type="text" name="age" placeholder="Age">
<input type="submit" name="submit" value="Register">
</form>
</div>
</body>
</html>

OUTPUT.PHP

<?php
$id=$_POST['id'];
$sname=$_POST['sname'];
$sdept=$_POST['sdept'];
$age=$_POST['age'];
$conn=new mysqli('localhost','root','','example');
if($conn->connect_error)
{
die('Connection Failed : '.$conn->connect_error);
}
else
{
$stmt = $conn->prepare("insert into
student(id,sname,sdept,age)values(?,?,?,?)");
$stmt->bind_param("issi",$id,$sname,$sdept,$age);
$stmt->execute();
echo "Registered Successfully...!";
$stmt->close();
$conn->close();
}
?>
OUTPUT:
12.CUSTOMER INFORMATION

PROGRAM:

<html>
<head>
<title>PHP program to fetch data from database in & show in html table</title>
</head>
<body>
<h3>Write a PHP program to fetch data from database and display in a HTML
table in php</h3>
<table border="2" bordercolor="blue" bgcolor="#00FF">
<thead>
<tr>
<th>ID</th>
<th>Customer_Name</th>
<th>Item</th>
<th>Phone_Number</th>
</tr>
</thead>
<tbody>
<?php
$connection = mysqli_connect("localhost","root","");
$db = mysqli_select_db($connection,"detail");
$query = mysqli_query($connection,"SELECT * FROM customer");
if (mysqli_num_rows($query)==0)
{
echo "<tr>
<td colspan='4'> No rows returned </td>
</tr>";
}
else
{
while($row=mysqli_fetch_row($query))
{
echo "<tr>
<td> $row[0] </td>
<td> $row[1] </td>
<td> $row[2] </td>
<td> $row[3] </td>
</tr> ";
}
}
?>
</tbody>
</table>
</body>
</html>
OUTPUT:

You might also like