Rahul PHP
Rahul PHP
Q1.) Write a PHP program to swap two numbers with and without
using third variable
<?php
$a = 45;
$b = 78;
$third = $a;
$a = $b;
$b = $third;
?>
<br><br>
<?php
$a=234;
$b=345;
$a=$a+$b;
$b=$a-$b;
$a=$a-$b;
?>
O/P:-
Q2.) Write a PHP program to find the factorial of a number
<?php
$num = 14597;
$sum=0; $rem=0;
$rem=$num%10;
$num=$num/10;
?>
O/P: -
Q3.) Write a PHP program to count the total number of words
in a string.
<?php
$len = str_word_count($str);
echo $len;
?>
O/P: -
Q4.) Write a program in PHP to find the occurrence of a word in a
string.
<?php
// a s2 appears in s1.
$res = substr_count($s1,$s2);
echo($res);
?>
O/P: -
Q5.) Write a PHP program to replace a word in a string.
<?php
// Given string
$w1 = "khurasane";
$w2 = "khurasane";
echo $str;
?>
O/P: -
Q6.) Write a PHP program to demonstrate various functions of
regular expression.
<html>
<body>
<?php
$pattern3 = "/microsoft/i";
?>
</body>
</html>
O/P :-
Q7.) Write a PHP program to find area of triangle and rectangle
using functions.
<?php
$triangleBase = 5;
$triangleHeight = 8;
$rectangleLength = 6;
$rectangleWidth = 10;
// Calculate areas
$rectangleArea = calculateRectangleArea($rectangleLength,
$rectangleWidth);
?>
O/P: -
Q8.) Write a PHP program to find the GCD of two numbers using
user defined function.
<?php
if ($y == 0)
return $x;
$x = 250;
$y = 475;
?>
O/P:-
Q9.) Write a Program for finding the biggest number in an array
without using any array Functions.
<?php
$lar = $a[0];
$s = count($a);
$lar = $a[$i];
?>
O/P:-
Q10. Write a Program for finding the smallest number in an array.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<?php
echo(min($numbers));
?>
</body>
</html>
O/P:-
Q11.) Write a PHP program to design a simple calculator.
<!DOCTYPE html>
<head>
</head>
<?php
$first_num = $_POST['first_num'];
$second_num = $_POST['second_num'];
$operator = $_POST['operator'];
$result = '';
switch ($operator) {
case "Add":
break;
case "Subtract":
break;
case "Multiply":
break;
case "Divide":
$result = $first_num / $second_num;
?>
<body>
<div id="page-wrap">
<p>
</p>
<p>
</p>
<p>
</p>
</form>
</div>
</body>
</html>
O/P: -
Q12. Design a simple web page to generate multiplication table for a given
number using PHP.
<!DOCTYPE html>
<html>
<body>
<center>
<h3>
</h3>
<form method="POST">
Enter a number:
<input type="Submit"
</form>
</center>
</body>
</html>
<?php
if($_POST) {
$num = $_POST["number"];
");
. $num * $i . "</p>
");
?>
O/P:-
Q13. Design a web page that should compute one’s age on a given date using
PHP.
<!DOCTYPE html>
<html>
<head>
<title>Age Calculator</title>
</head>
<body>
<h2>Age Calculator</h2>
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$targetdate = $_POST["targetdate"];
// Calculate age
$age = $diff->format('%y');
// Display age
?>
</body>
</html>
O/P: -
Q14. Write a PHP program read Student information (Roll_No, Name, Class,
Contact_No, email_id) using HTML form and display this information using
GET/POST method.
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<label for="name">Name:</label>
<label for="class">Class:</label>
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$rollno = $_POST["rollno"];
$name = $_POST["name"];
$class = $_POST["class"];
$contact = $_POST["contact"];
$email = $_POST["email"];
?>
</body>
</html>
O/p:-
Q15. Write a PHP program to read student marks for semester
subjects with other required details (prn, name, rollno, class ….etc.)
and display semester mark list.
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<label for="prn">PRN:</label>
<label for="name">Name:</label>
<label for="class">Class:</label>
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$prn = $_POST["prn"];
$name = $_POST["name"];
$rollno = $_POST["rollno"];
$class = $_POST["class"];
$subject1 = $_POST["subject1"];
$subject2 = $_POST["subject2"];
$subject3 = $_POST["subject3"];
$subject4 = $_POST["subject4"];
?>
</body>
</html>
O/P: -