0% found this document useful (0 votes)
17 views

Php and MySQL Lab Part A

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)
17 views

Php and MySQL Lab Part A

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/ 11

ST.

Joseph’s Degree college, Hunsur


Department of Computer Application
PHP and MySQL LAB

Sl Part A
No
1 Write a PHP script to Print “Hello world”.

2 Write a PHP script to find odd or even numbers from a given


number.
3 Write a PHP script to find the maximum of three number

4 Write a PHP script to swap two numbers.

5 Write a PHP script to find the factorial of a number

Write a PHP script to check whether a given number is


6
palindrome or not
7 Write a PHP script to reverse a given number and calculate its
sum
Write a PHP script to generate a Fibonacci series using a
8
recursive function
9 Write a PHP script to implement at least seven string functions.

Write a PHP script to insert a new item in an array on any position


10
in PHP
11 Write a PHP script to implement the constructor and destructor.

Aishwarya, Assistant professor, St. Joseph’s college, hunsur


1.Write a PHP script to Print “Hello world”.
<?php
echo "<h1>Welcome</h1>";
print "<p style='font-weight: bold; color:blue;'>Hello world</p>";
?>

2. Write a PHP script to find odd or even numbers from a given number.
<!DOCTYPE html>
<html>
<head>
<title>Odd or Even Number Checker</title>
</head>
<body>
<h2>Odd or Even Number Checker</h2>
<form method="post" action="">
Enter a number: <input type="number" name="number" required>
<input type="submit" value="Check">
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST["number"])) {
$inputNumber = $_POST["number"];
if ($inputNumber % 2 == 0) {
echo "<p>{$inputNumber} is an even number.</p>";
} else {
echo "<p>{$inputNumber} is an odd number.</p>";
}
}
?>
</body>
</html>

Aishwarya, Assistant professor, St. Joseph’s college, hunsur


3.Write a PHP script to find the maximum of three number
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Retrieve input values from the form
$num1 = $_POST['num1'];
$num2 = $_POST['num2'];
$num3 = $_POST['num3'];
$max = max($num1, $num2, $num3);
}
?>
<!-- HTML Form to accept numbers from the user -->
<form method="POST" action="">
<label for="num1">Enter first number:</label>
<input type="number" id="num1" name="num1" required><br><br>

<label for="num2">Enter second number:</label>


<input type="number" id="num2" name="num2" required><br><br>

<label for="num3">Enter third number:</label>


<input type="number" id="num3" name="num3" required><br><br>

<input type="submit" value="Find Maximum">


</form>
<?php
if (isset($max)) {
echo "<p>The maximum of $num1, $num2, and $num3 is: $max</p>";
}
?>

4.Write a PHP script to swap two numbers.

Aishwarya, Assistant professor, St. Joseph’s college, hunsur


<?php
function swapNumbers(&$num1, &$num2) {
$temp = $num1;
$num1 = $num2;
$num2 = $temp;
}
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$number1 = $_POST['num1'];
$number2 = $_POST['num2'];
swapNumbers($number1, $number2);
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Swap Two Numbers</title>
</head>
<body>
<!-- Form to get input numbers -->
<form method="POST" action="">
<label for="num1">Enter first number:</label>
<input type="number" id="num1" name="num1" required><br><br>
<label for="num2">Enter second number:</label>
<input type="number" id="num2" name="num2" required><br><br>
<input type="submit" value="Swap Numbers">
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {

Aishwarya, Assistant professor, St. Joseph’s college, hunsur


echo "<h3>Before swapping:</h3>";
echo "Number 1: " . $_POST['num1'] . "<br>";
echo "Number 2: " . $_POST['num2'] . "<br>";
echo "<h3>After swapping:</h3>";
echo "Number 1: " . $number1 . "<br>";
echo "Number 2: " . $number2 . "<br>";
}
?>
</body>
</html>

5. Write a PHP script to find the factorial of a number.


<!DOCTYPE html>
<html>
<head>
<title>Factorial Calculator</title>
</head>
<body>
<h2>Factorial Calculator</h2>
<form method="post" action="">
Enter a number: <input type="number" name="number" required>
<input type="submit" value="Calculate Factorial">
</form>
<?php
// Define the factorial function
function factorial($n) {
if ($n === 0) {
return 1;
} else {
return $n * factorial($n - 1);
}

Aishwarya, Assistant professor, St. Joseph’s college, hunsur


}
// Handle the POST request
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$inputNumber = $_POST["number"];
$result = factorial($inputNumber);
echo "<p>The factorial of $inputNumber is: $result</p>";
}
?>
</body>

</html>

6.Write a PHP script to check whether a given number is palindrome or not.


<!DOCTYPE html>
<html>
<head>
<title>Palindrome Checker</title>
</head>
<body>
<h2>Palindrome Checker</h2>
<form method="post" action="">
Enter a number: <input type="number" name="number" required>
<input type="submit" value="Check Palindrome">
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Retrieve the input number from the form
$inputNumber = $_POST["number"];
// Convert the number to a string and reverse it
$reverseNumber = strrev((string)$inputNumber);
// Check if the original and reversed numbers are the same
if ($inputNumber == $reverseNumber) {

Aishwarya, Assistant professor, St. Joseph’s college, hunsur


echo "<p>{$inputNumber} is a palindrome number.</p>";
} else {
echo "<p>{$inputNumber} is not a palindrome number.</p>";
}
}
?>
</body>
</html>

7.Write a PHP script to reverse a given number and calculate its sum.
<!DOCTYPE html>
<html>
<head>
<title>Number Reversal and Digit Sum</title>
</head>
<body>
<h2>Number Reversal and Digit Sum</h2>
<form method="post" action="">
Enter a number: <input type="number" name="number" required> <input type="submit"
value="Reverse and Calculate Sum"> </form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Retrieve the input number from the form
$inputNumber = $_POST["number"];
// Reverse the number
$reversedNumber = strrev($inputNumber);
// Calculate the sum of the digits
$sum = 0;
$length = strlen($reversedNumber); for ($i = 0; $i < $length; $i++) { $sum +=
(int)$reversedNumber[$i];
}
// Display the reversed number and the sum of its digits

Aishwarya, Assistant professor, St. Joseph’s college, hunsur


echo "<p>Reversed Number: $reversedNumber</p>";
echo "<p>Sum of Digits: $sum</p>";
}
?>
</body> </html>

8.Write a PHP script to generate a Fibonacci series using a recursive


function.
<!DOCTYPE html>
<html>
<head>
<title>Fibonacci Series</title>
</head>
<body>
<h2>Fibonacci Series</h2>
<form method="post" action="">
Enter the number of terms: <input type="number" name="terms" required>
<input type="submit" value="Generate Fibonacci Series">
</form>

<?php
// Fibonacci function to generate the nth term
function fibonacci($n) {
if ($n == 0) {
return 0;
} elseif ($n == 1) {
return 1;
} else {
return (fibonacci($n - 1) + fibonacci($n - 2));
}
}

Aishwarya, Assistant professor, St. Joseph’s college, hunsur


// Check if form was submitted and the number of terms is set
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Retrieve the number of terms from the form input
$numTerms = $_POST["terms"];

// Validate if the number of terms is a valid positive integer


if ($numTerms > 0) {
// Display Fibonacci series
echo "<p>Fibonacci Series with $numTerms terms:</p>";
for ($i = 0; $i < $numTerms; $i++) {
echo fibonacci($i) . " ";
}
} else {
echo "<p>Please enter a positive integer for the number of terms.</p>";
}
}
?>
</body>
</html>

9.Write a PHP script to implement at least seven string functions.


<!DOCTYPE html>
<html>
<head>
<title>String Functions Demo</title>
</head>
<body>
<h2>String Functions Demo</h2>
<?php
// Sample string
$string = "Hello, World!";

Aishwarya, Assistant professor, St. Joseph’s college, hunsur


// 1. String Length
echo "<p>1. String Length: " . strlen($string) . "</p>";
// 2. Convert to Uppercase
echo "<p>2. Convert to Uppercase: " . strtoupper($string) . "</p>";
// 3. Convert to Lowercase
echo "<p>3. Convert to Lowercase: " . strtolower($string) . "</p>";
// 4. Substring
$start = 0;
$length = 5;
echo "<p>4. Substring (first 5 characters): " . substr($string, $start, $length) . "</p>";
// 5. String Replace
$old = "World";
$new = "PHP";
$newString = str_replace($old, $new, $string); echo "<p>5. String Replace: " . $newString .
"</p>"; // 6. String Position (strpos)
$search = "World";
$position = strpos($string, $search); echo "<p>6. String Position (strpos): $search found at
position $position</p>";
// 7. Trim Whitespace
$whitespaceString = " Trim Me "; $trimmedString = trim($whitespaceString);
echo "<p>7. Trim Whitespace: '$whitespaceString' trimmed to '$trimmedString'</p>"; ?>
</body> </html>

10. Write a PHP script to insert a new item in an array on any position in
PHP.
<?php
$originalArray = array("apple", "banana", "cherry", "date");
echo "Original Array: ";
print_r($originalArray);
$newItem = "orange";
$positionToInsert = 2;
array_splice($originalArray, $positionToInsert, 0, $newItem);

Aishwarya, Assistant professor, St. Joseph’s college, hunsur


echo "Updated Array: ";
print_r($originalArray);
?>

11. Write a PHP script to implement the constructor and destructor.


<?php
class MyClass {
public $name;
public function __construct($name) {
echo "Constructing {$name}\n";
$this->name = $name;
}
public function __destruct() {
echo "Destructing {$this->name}\n";
}
public function greet() {
echo "Hello, {$this->name}!\n";
}
}
$obj1 = new MyClass("Object 1");
$obj1->greet();
$obj2 = new MyClass("Object 2");
$obj2->greet();
unset($obj1);
unset($obj2);
?>

Aishwarya, Assistant professor, St. Joseph’s college, hunsur

You might also like