User:Ravanlanki

1. Write a PHP script to print “hello world”. 

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

3. Write a PHP script to find maximum of three numbers. = $number2 && $number1 >= $number3) { $maxNumber = $number1; } elseif ($number2 >= $number1 && $number2 >= $number3) { $maxNumber = $number2; } else { $maxNumber = $number3; } // Output the maximum number echo "The maximum of {$number1}, {$number2}, and {$number3} is: {$maxNumber}"; ?>

4. Write a PHP script to swap two numbers.



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

6. Write a PHP script to check whether given number is palindrome or not. <?php // Function to check if a string is a palindrome function isPalindrome($str) { // Remove non-alphanumeric characters and convert to lowercase $clean_str = strtolower(preg_replace('/[^a-zA-Z0-9]/', '', $str)); // Reverse the string $reversed_str = strrev($clean_str); // Check if the original string is equal to its reverse if ($clean_str == $reversed_str) { return true; } else { return false; } } // Test the function with a string $string = "A man, a plan, a canal, Panama"; if (isPalindrome($string)) { echo "$string is a palindrome."; } else { echo "$string is not a palindrome."; } ?>

7. Write a PHP script to reverse a given number and calculate its sum.  0) { $digit = $number % 10; $reversed_number = $reversed_number * 10 + $digit; $number = (int)($number / 10); } return $reversed_number; } function sum_of_digits($number) { $sum = 0; while ($number > 0) { $digit = $number % 10; $sum += $digit; $number = (int)($number / 10); } return $sum; } $given_number = 87954; $reversed_number = reverse_number($given_number); echo "Reversed number: ". $reversed_number. " "; $sum_of_digits = sum_of_digits($given_number); echo "Sum of digits: ". $sum_of_digits; ?>

8. Write a PHP script to generate a Fibonacci series using Recursive function.



9. Write a PHP script to implement at least seven string functions. <?php // Sample string for demonstration $string = "Hello, World!"; // 1. strlen: Get the length of a string $length = strlen($string); echo "1. Length of the string: $length "; // 2. strtoupper: Convert a string to uppercase $uppercaseString = strtoupper($string); echo "2. Uppercase string: $uppercaseString "; // 3. strtolower: Convert a string to lowercase $lowercaseString = strtolower($string); echo "3. Lowercase string: $lowercaseString "; // 4. substr: Get a part of a string $substring = substr($string, 0, 5); // Get first 5 characters echo "4. Substring: $substring "; // 5. str_replace: Replace occurrences of a string $newString = str_replace("World", "PHP", $string); echo "5. After replacement: $newString "; // 6. strpos: Find the position of the first occurrence of a substring $position = strpos($string, "World"); echo "6. Position of 'World': $position "; // 7. strrev: Reverse a string $reversedString = strrev($string); echo "7. Reversed string: $reversedString "; ?>

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

11. Write a PHP script to implement constructor and destructor. someMethod; // Object will be destroyed automatically when it goes out of scope or unset ?>

12. Write a PHP script to implement form handling using get method.

<!DOCTYPE html> GET Form Example  Name:  Email:   Process.php 

13. Write a PHP script to implement form handling using post method. <!DOCTYPE html> POST Form Example  Name:  Email: <input type="email" id="email" name="email"> <input type="submit" value="Submit"> Process.php <?php // Check if form data has been submitted if ($_SERVER["REQUEST_METHOD"] == "POST") { // Retrieve form data $name = $_POST['name']; $email = $_POST['email']; // Display submitted data echo "Name: ". $name. " "; echo "Email: ". $email. " "; } else { echo "Form data not submitted."; } ?>

14. Write a PHP script that receive form input by the method post to check the number is prime or not. <!DOCTYPE html> Check Prime Number Check Prime Number <form action="check_prime.php" method="post"> Enter a number: <input type="number" name="number" required> <input type="submit" value="Check Prime"> Check_prime.php <?php if ($_SERVER["REQUEST_METHOD"] == "POST") { // Retrieve number from form input $number = $_POST['number']; // Check if the number is prime $is_prime = true; if ($number <= 1) { $is_prime = false; } else { for ($i = 2; $i < $number; $i++) { if ($number % $i == 0) { $is_prime = false; break; } } } // Display the result if ($is_prime) { echo "The number ". $number. " is prime."; } else { echo "The number ". $number. " is not prime."; } } ?>

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

<!DOCTYPE html> Receive String Input Receive String Input <form action="receive_string.php" method="post"> Enter a string: <input type="text" name="input_string" required> <input type="submit" value="Submit"> Receive_string.php <?php if ($_SERVER["REQUEST_METHOD"] == "POST") { // Retrieve string from form input $input_string = $_POST['input_string']; // Display the string echo "You entered: ". $input_string; } ?>

16. Write a PHP script to compute addition of two matrices as a form input. <!DOCTYPE html> Matrix Addition Matrix Addition <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post"> Enter the elements of the first matrix: Matrix 1, Row 1: <input type="number" name="m1r1c1" required> <input type="number" name="m1r1c2" required> Matrix 1, Row 2: <input type="number" name="m1r2c1" required> <input type="number" name="m1r2c2" required> Enter the elements of the second matrix: Matrix 2, Row 1: <input type="number" name="m2r1c1" required> <input type="number" name="m2r1c2" required> Matrix 2, Row 2: <input type="number" name="m2r2c1" required> <input type="number" name="m2r2c2" required> <input type="submit" value="Add Matrices"> <?php if ($_SERVER["REQUEST_METHOD"] == "POST") { // Retrieve elements of the first matrix $m1r1c1 = $_POST['m1r1c1']; $m1r1c2 = $_POST['m1r1c2']; $m1r2c1 = $_POST['m1r2c1']; $m1r2c2 = $_POST['m1r2c2']; // Retrieve elements of the second matrix $m2r1c1 = $_POST['m2r1c1']; $m2r1c2 = $_POST['m2r1c2']; $m2r2c1 = $_POST['m2r2c1']; $m2r2c2 = $_POST['m2r2c2']; // Compute addition of matrices $result = array( array($m1r1c1 + $m2r1c1, $m1r1c2 + $m2r1c2), array($m1r2c1 + $m2r2c1, $m1r2c2 + $m2r2c2) ); // Display the result echo " Matrix Addition Result: "; echo " "; } ?>

17. PHP script to show the functionality of date and time function. <?php // Set the default timezone date_default_timezone_set('UTC'); // Get the current date and time $current_datetime = date('Y-m-d H:i:s'); echo "Current date and time (UTC): ". $current_datetime. " "; // Get the current date in a specific format $current_date = date('Y-m-d'); echo "Current date: ". $current_date. " "; // Get the current time in a specific format $current_time = date('H:i:s'); echo "Current time: ". $current_time. " "; // Get the current day of the week (0 for Sunday through 6 for Saturday) $current_day_of_week = date('w'); $days_of_week = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]; echo "Day of the week: ". $days_of_week[$current_day_of_week]. " "; // Get the current month (1 for January through 12 for December)$current_month = date('n'); $months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]; echo "Current month: ". $months[$current_month - 1]. " "; // Get the current year $current_year = date('Y'); echo "Current year: ". $current_year. " "; ?>

18. Write a PHP script to implement database creation. <?php // Database configuration $servername = "localhost"; // Change this to your database server $username = "root"; // Change this to your database username $password = ""; // Change this to your database password // Create connection $conn = new mysqli($servername, $username, $password); // Check connection if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } // Create database $sql = "CREATE DATABASE IF NOT EXISTS Abhishek"; // Change 'my_database' to your desired database name if ($conn->query($sql) === TRUE) { echo "Database created successfully"; } else { echo "Error creating database: ". $conn->error; } // Close connection $conn->close; ?>

19. Write a PHP script to create table. <?php // Database configuration $servername = "localhost"; // Change this to your database server $username = "root"; // Change this to your database username $password = ""; // Change this to your database password $database = "abhishek"; // Change this to your database name // Create connection $conn = new mysqli($servername, $username, $password, $database); // Check connection if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } // SQL to create table $sql = "CREATE TABLE IF NOT EXISTS users ( id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, firstname VARCHAR(30) NOT NULL, lastname VARCHAR(30) NOT NULL, email VARCHAR(50), reg_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP )"; // Execute SQL query if ($conn->query($sql) === TRUE) { echo "Table users created successfully"; } else { echo "Error creating table: ". $conn->error; } // Close connection $conn->close; ?>