Rohitweb Program
Rohitweb Program
2 Display the day of the week based on a number (1-7) using switch. 2
<?php
$number = 7; // You can change this number
if ($number % 2 == 0) {
echo "$number is even.";
} else {
echo "$number is odd.";
}
?>
Output:- 7 is odd.
1
2. Display the day of the week based on a number (1-7)
using switch.
<?php
$dayNumber = 4; // You can change this value from 1 to 7
switch ($dayNumber) {
case 1:
echo "Monday";
break;
case 2:
echo "Tuesday";
break;
case 3:
echo "Wednesday";
break;
case 4:
echo "Thursday";
break;
case 5:
echo "Friday";
break;
case 6:
echo "Saturday";
break;
case 7:
echo "Sunday";
break;
default:
echo "Invalid number! Please enter a number between 1 and 7.";
}
?>
Output:- Thursday
2
3. Create a program to swap two numbers without a
temporary variable.
<?php
$a = 5;
$b = 10;
3
4. Sort an indexed array in ascending and descending
order.
<?php
$numbers = [25, 10, 5, 40, 30];
4
5. Write a function to check if a number is prime.
<?php
function isPrime($num) {
if ($num <= 1) return false;
if ($num == 2) return true;
if (isPrime($number)) {
echo "$number is a prime number.";
} else {
echo "$number is not a prime number.";
}
?>
5
6. Write a script to send an email using PHP’s mail()
function.
<?php
$to = "[email protected]"; // Replace with the recipient's email
address
$subject = "Test Email from PHP";
$message = "Hello! This is a test email sent using PHP's mail()
function.";
$headers = "From: [email protected]"; // Replace with your sender
email
6
7. Create a database students and a table users (id, name,
email).
<?php
$servername = "localhost";
$username = "root"; // default username for XAMPP/WAMP
$password = ""; // default password is empty for XAMPP
$dbname = "students";
// 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 $dbname";
if ($conn->query($sql) === TRUE) {
echo "Database 'students' created successfully.<br>";
} else {
echo "Error creating database: " . $conn->error;
}
// Create table
$tableSql = "CREATE TABLE IF NOT EXISTS users (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(50) NOT NULL,
email VARCHAR(100) NOT NULL UNIQUE
)";
$conn->close();
?>
Output:- Database 'students' created successfully.
Table 'users' created successfully.
7
8. Print Fibonacci series up to n terms using a for loop.
<?php
$n = 10; // Number of terms in the Fibonacci series
$first = 0;
$second = 1;
8
9. Extract all URLs from a given string using regex.
<?php
$string = "Here are some links: https://example.com,
http://www.test.com, and ftp://files.example.com.";
9
10. Create a PHP script that demonstrates type juggling
(implicit type conversion).
<?php
$number = 10; // Integer
$text = "5"; // String
echo "The result of adding number and text is: $result<br>"; // Output:
15
echo "The result of adding integer and float is: $sum<br>"; // Output:
13.5
10