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

Rohitweb Program

The document is a practical file for a Web Programming course at Noida International University, detailing various programming tasks and their solutions in PHP. It includes tasks such as checking if a number is even or odd, displaying the day of the week, swapping numbers, sorting arrays, checking for prime numbers, sending emails, creating a database, printing Fibonacci series, extracting URLs using regex, and demonstrating type juggling. Each task is accompanied by sample code and output results.

Uploaded by

rohitchoud181
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

Rohitweb Program

The document is a practical file for a Web Programming course at Noida International University, detailing various programming tasks and their solutions in PHP. It includes tasks such as checking if a number is even or odd, displaying the day of the week, swapping numbers, sorting arrays, checking for prime numbers, sending emails, creating a database, printing Fibonacci series, extracting URLs using regex, and demonstrating type juggling. Each task is accompanied by sample code and output results.

Uploaded by

rohitchoud181
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Noida International University

Department of Computer Science


Practical File – Web Programming
2024-2025

Submitted By: Submitted To:


Rohit kumar Mr. Konark Saxena
BCA-II Sem, Sec-‘D’
NIU-24-19809
Index

Sr Question Page Sign.


No. No
1 Check if a number is even or odd using if-else. 1

2 Display the day of the week based on a number (1-7) using switch. 2

3 Create a program to swap two numbers without a temporary 3


variable.
4 Sort an indexed array in ascending and descending order. 4

5 Write a function to check if a number is prime. 5

6 Write a script to send an email using PHP’s mail() function. 6

7 Create a database students and a table users (id, name, email). 7

8 Print Fibonacci series up to n terms using a for loop. 8

9 Extract all URLs from a given string using regex. 9

10 Create a PHP script that demonstrates type juggling (implicit type 10


conversion).
1. Check if a number is even or odd using if-else.

<?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;

echo "Before Swapping:<br>";


echo "a = $a, b = $b<br>";

// Swapping without using a temporary variable


$a = $a + $b; // a becomes 15
$b = $a - $b; // b becomes 5
$a = $a - $b; // a becomes 10

echo "After Swapping:<br>";


echo "a = $a, b = $b";
?>

Output:- Before Swapping:


a = 5, b = 10
After Swapping:
a = 10, b = 5

3
4. Sort an indexed array in ascending and descending
order.

<?php
$numbers = [25, 10, 5, 40, 30];

echo "Original Array:<br>";


print_r($numbers);

// Sort in ascending order


sort($numbers);
echo "<br><br>Array in Ascending Order:<br>";
print_r($numbers);

// Sort in descending order


rsort($numbers);
echo "<br><br>Array in Descending Order:<br>";
print_r($numbers);
?>

Output:- Original Array:


Array ( [0] => 25 [1] => 10 [2] => 5 [3] => 40 [4] => 30 )

Array in Ascending Order:


Array ( [0] => 5 [1] => 10 [2] => 25 [3] => 30 [4] => 40 )

Array in Descending Order:


Array ( [0] => 40 [1] => 30 [2] => 25 [3] => 10 [4] => 5 )

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;

for ($i = 2; $i <= sqrt($num); $i++) {


if ($num % $i == 0) {
return false;
}
}
return true;
}

// Test the function


$number = 13;

if (isPrime($number)) {
echo "$number is a prime number.";
} else {
echo "$number is not a prime number.";
}
?>

Output:- 13 is 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

if (mail($to, $subject, $message, $headers)) {


echo "Email sent successfully!";
} else {
echo "Failed to send email.";
}
?>

Output:- Email sent successfully!

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;
}

// Select the database


$conn->select_db($dbname);

// 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
)";

if ($conn->query($tableSql) === TRUE) {


echo "Table 'users' created successfully.";
} else {
echo "Error creating table: " . $conn->error;
}

$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;

echo "Fibonacci series up to $n terms:<br>";

for ($i = 0; $i < $n; $i++) {


echo $first . " ";

// Generate next term


$next = $first + $second;
$first = $second;
$second = $next;
}
?>

Output:- Fibonacci series up to 10 terms:


0 1 1 2 3 5 8 13 21 34

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.";

$pattern = "/\bhttps?:\/\/\S+/i"; // Regex to match http or https URLs

preg_match_all($pattern, $string, $matches);

echo "Extracted URLs:<br>";


foreach ($matches[0] as $url) {
echo $url . "<br>";
}
?>

Output:- Extracted URLs:


https://example.com
http://www.test.com

9
10. Create a PHP script that demonstrates type juggling
(implicit type conversion).

<?php
$number = 10; // Integer
$text = "5"; // String

// Implicit type conversion happens here


$result = $number + $text; // PHP converts the string '5' to an integer
and adds it to $number

echo "The result of adding number and text is: $result<br>"; // Output:
15

$floatValue = 3.5; // Float


$sum = $number + $floatValue; // PHP implicitly converts $number to
float

echo "The result of adding integer and float is: $sum<br>"; // Output:
13.5

$booleanValue = true; // Boolean


$sum2 = $number + $booleanValue; // PHP converts 'true' to 1 and adds
it to $number

echo "The result of adding integer and boolean is: $sum2<br>"; //


Output: 11
?>

Output:- The result of adding number and text is: 15


The result of adding integer and float is: 13.5
The result of adding integer and boolean is: 11

10

You might also like