0% found this document useful (0 votes)
21 views15 pages

1708583448873_php programs

mm

Uploaded by

raghavendra
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)
21 views15 pages

1708583448873_php programs

mm

Uploaded by

raghavendra
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/ 15

1 Write a PHPscript to print “hello world”.

<?php
echo "Hello, World!";
?>

2 Write a PHPscript to find odd or even number from given


number.
<?php
$number = 7; // Replace with your number
if ($number % 2 == 0) {
echo "Even";
} else {
echo "Odd";
}
?>

3 Write a PHPscript to find maximum of three numbers.


<?php
$num1 = 5;
$num2 = 10;
$num3 = 8;
$max = max($num1, $num2, $num3);
echo "Maximum number is: $max";
?>

4 Write a PHPscript to swap two numbers.


<?php
$a = 5;
$b = 10;
echo "Before swap: a = $a, b = $b";
$temp = $a;
$a = $b;
$b = $temp;
echo "After swap: a = $a, b = $b";
?>

5 Write a PHPscript to find the factorial of a number.


<?php
$number = 5; // Replace with your number
$factorial = 1;
for ($i = 1; $i <= $number; $i++) {
$factorial *= $i;
}
echo "Factorial of $number is: $factorial";
?>

6 Write a PHPscript to check whether given number is


palindrome or not.
<?php
$number = 121; // Replace with your number
$originalNumber = $number;
$reverse = 0;
while ($number != 0) {
$reverse = $reverse * 10 + $number % 10;
$number = (int)($number / 10);
}
if ($originalNumber == $reverse) {
echo "Palindrome";
} else {
echo "Not Palindrome";
}
?>

7 Write a PHP script to reverse a given number and


calculate its sum.
<?php
$number = 123; // Replace with your number
$originalNumber = $number;
$reverse = 0;
$sum = 0;
while ($number != 0) {
$digit = $number % 10;
$reverse = $reverse * 10 + $digit;
$sum += $digit;
$number = (int)($number / 10);
}
echo "Reversed Number: $reverse, Sum of Digits: $sum";
?>

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


Recursive function.
<?php
function fibonacci($n) {
if ($n <= 1) {
return $n;
}
return fibonacci($n - 1) + fibonacci($n - 2);
}
$terms = 10; // Replace with the number of terms you want
for ($i = 0; $i < $terms; $i++) {
echo fibonacci($i) . " ";
}
?>

9 Write a PHP script to implement atleast seven string


functions.
<?php
$string = "Hello, World!";
// String Length
echo "Length: " . strlen($string) . "<br>";
// Word Count
echo "Word Count: " . str_word_count($string) . "<br>";
// Reverse String
echo "Reversed: " . strrev($string) . "<br>";
// Convert to Uppercase
echo "Uppercase: " . strtoupper($string) . "<br>";
// Convert to Lowercase
echo "Lowercase: " . strtolower($string) . "<br>";
// Substring
echo "Substring: " . substr($string, 0, 5) . "<br>";
// Replace
echo "Replaced: " . str_replace("World", "PHP", $string) .
"<br>";
?>

10 Write a PHP program to insert new item in array on any


position in PHP.
<?php
$array = [1, 2, 3, 4, 5];
$newItem = 6;
$position = 2; // Replace with desired position
array_splice($array, $position, 0, $newItem);
print_r($array);
?>

11 Write a PHP script to implement constructor and


destructor.
<?php
class MyClass {
public function __construct() {
echo "Constructor called<br>";
}
public function someMethod() {
echo "Method called<br>";
}
public function __destruct() {
echo "Destructor called<br>";
}
}
$obj = new MyClass();
$obj->someMethod();
unset($obj); // Destructor will be called when object is unset
?>

12 Write a PHP script to implement form handling using get


method.
<!-- HTML Form -->
<form action="process_get.php" method="get">
Name: <input type="text" name="name">
<input type="submit" value="Submit">
</form>
<!-- process_get.php -->
<?php
$name = $_GET['name'];
echo "Hello, $name!";
?>

13 Write a PHP script to implement form handling using


post method.
<!-- HTML Form -->
<form action="process_post.php" method="post">
Name: <input type="text" name="name">
<input type="submit" value="Submit">
</form>
<!-- process_post.php -->
<?php
$name = $_POST['name'];
echo "Hello, $name!";
?>

14 Write a PHP script that receive form input by the method


post to check the number is prime or not.
<!-- HTML Form -->
<form action="check_prime.php" method="post">
Number: <input type="text" name="number">
<input type="submit" value="Check Prime">
</form>
<!-- check_prime.php -->
<?php
$number = $_POST['number'];
$isPrime = true;
for ($i = 2; $i <= sqrt($number); $i++) {
if ($number % $i == 0) {
$isPrime = false;
break;
}
}
if ($isPrime) {
echo "$number is Prime.";
} else {
echo "$number is not Prime.";
}
?>

15 Write a PHP script that receive string as a form input.


<!-- HTML Form -->
<form action="process_string.php" method="post">
String: <input type="text" name="inputString">
<input type="submit" value="Submit">
</form>
<!-- process_string.php -->
<?php
$inputString = $_POST['inputString'];
echo "Received String: $inputString";
?>

16 Write a PHP script to compute addition of two matrices


as a form input.
<!-- HTML Form -->
<form action="add_matrices.php" method="post">
Matrix A: <input type="text" name="matrixA">
Matrix B: <input type="text" name="matrixB">
<input type="submit" value="Add Matrices">
</form>
<!-- add_matrices.php -->
<?php
$matrixA = $_POST['matrixA'];
$matrixB = $_POST['matrixB'];
// Implement matrix addition logic
// ...
echo "Resultant Matrix: ...";
?>

17 Write a PHP script to show the functionality of date and


time function.
<?php
echo "Current Date: " . date("Y-m-d") . "<br>";
echo "Current Time: " . date("H:i:s") . "<br>";
?>

18 Write a PHP program to upload a file.


<!-- HTML Form -->
<form action="upload_file.php" method="post"
enctype="multipart/form-data">
Select File: <input type="file" name="file">
<input type="submit" value="Upload">
</form>
<!-- upload_file.php -->
<?php
$targetDirectory = "uploads/";
$targetFile = $targetDirectory .
basename($_FILES["file"]["name"]);
move_uploaded_file($_FILES["file"]["tmp_name"],
$targetFile);
echo "File uploaded successfully.";
?>

19 Write a PHP script to implement database creation.


<?php
$servername = "localhost";
$username = "root";
$password = "";
$conn = new mysqli($servername, $username, $password);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$databaseName = "mydatabase";
$sql = "CREATE DATABASE IF NOT EXISTS $databaseName";
if ($conn->query($sql) === TRUE) {
echo "Database created successfully";
} else {
echo "Error creating database: " . $conn->error;
}
$conn->close();
?>

20 Write a PHP script to create table.


<?php
$servername = "localhost";
$username = "root";
$password = "";
$databaseName = "mydatabase";
$conn = new mysqli($servername, $username, $password,
$databaseName);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "CREATE TABLE IF NOT EXISTS students (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL,
age INT,
course VARCHAR(50)
)";
if ($conn->query($sql) === TRUE) {
echo "Table created successfully";
} else {
echo "Error creating table: " . $conn->error;
}
$conn->close();
?>

21 Develop a PHP program to design a college admission


form using MYSQL database.
<!-- HTML Form -->
<form action="process_admission_form.php"
method="post">
Name: <input type="text" name="name"><br>
Age: <input type="number" name="age"><br>
Course: <input type="text" name="course"><br>
<input type="submit" value="Submit">
</form>
<!-- process_admission_form.php -->
<?php
$servername = "localhost";
$username = "root";
$password = "";
$databaseName = "mydatabase";
$conn = new mysqli($servername, $username, $password,
$databaseName);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$name = $_POST['name'];
$age = $_POST['age'];
$course = $_POST['course'];
$sql = "INSERT INTO students (name, age, course) VALUES
('$name', $age, '$course')";
if ($conn->query($sql) === TRUE) {
echo "Admission form submitted successfully";
} else {
echo "Error submitting admission form: " . $conn->error;
}
$conn->close();
?>

You might also like