0% found this document useful (0 votes)
28 views23 pages

Assigment

The document contains 10 code snippets in PHP with explanations and outputs. The code snippets include functions to calculate sum of digits, check prime numbers, even/odd numbers, print tables, factorials, reverse numbers, reverse strings, swap numbers with and without temp variable, perform basic math operations using functions, and use PHP SESSION variable.

Uploaded by

Utkarsh Miglani
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)
28 views23 pages

Assigment

The document contains 10 code snippets in PHP with explanations and outputs. The code snippets include functions to calculate sum of digits, check prime numbers, even/odd numbers, print tables, factorials, reverse numbers, reverse strings, swap numbers with and without temp variable, perform basic math operations using functions, and use PHP SESSION variable.

Uploaded by

Utkarsh Miglani
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/ 23

Ques1)Write a PHP program to print the sum of

digits.
CODE

<?php

function sumOfDigits($number) {
$sum = 0;

while ($number > 0) {


$digit = $number % 10;
$sum += $digit;
$number = (int)($number / 10);
}

return $sum;
}
$number = 12345;
$result = sumOfDigits($number);
echo "The sum of digits of $number is: $result";
?>
OUTPUT
Ques 2)Write a PHP program to check prime
numbers.
CODE

<?php
function isPrime($number) {
if ($number <= 1) {
return false;
}

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


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

return true;
}
$testNumber = 17;

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

?>
OUTPUT
Ques 3)Write a PHP program to check even or
odd numbers.
CODE
<?php

function checkEvenOrOdd($number) {
if ($number % 2 == 0) {
return "Even";
} else {
return "Odd";
}
}

$testNumber = 7;

$result = checkEvenOrOdd($testNumber);

echo "$testNumber is $result.";

?>
OUTPUT
Ques 4)Write a PHP program to print tables of
numbers.
CODE
<?php

function printTable($number, $limit = 10) {


echo "Multiplication table for $number:\n";

for ($i = 1; $i <= $limit; $i++) {


$result = $number * $i;
echo "$number x $i = $result\n";
}
}

// Example usage
$tableNumber = 5;
printTable($tableNumber);

?>
OUTPUT
Ques 5)Write a PHP program to print factorials
of a number.
CODE
<?php

function factorial($number) {
if ($number < 0) {
return "Undefined (factorial is not defined for negative
numbers)";
} elseif ($number == 0 || $number == 1) {
return 1;
} else {
return $number * factorial($number - 1);
}
}
$number = 5;

$result = factorial($number);

echo "Factorial of $number is: $result";

?>
OUTPUT
Ques 6)Write a PHP program to reverse a given
number.
CODE
<?php

function reverseNumber($number) {
$reverse = 0;

while ($number > 0) {


$digit = $number % 10;
$reverse = $reverse * 10 + $digit;
$number = (int)($number / 10);
}

return $reverse;
}
$number = 12345;

$reversedNumber = reverseNumber($number);

echo "Original number: $number\n";


echo "Reversed number: $reversedNumber";

?>
OUTPUT
Ques 7)Write a PHP program to reverse a given
string.
CODE

<?php
function reverseString($inputString) {
return strrev($inputString);
}
$input = "Hello, World!";
$reversedString = reverseString($input);

echo "Original String: $input<br>";


echo "Reversed String: $reversedString";
?>
OUTPUT
Ques 8)Write a PHP program to swap two
numbers with and without using a third variable.
CODE

<?php
function swapWithTemp($num1, $num2) {
echo "Before swapping: num1 = $num1, num2 = $num2\n";

$temp = $num1;
$num1 = $num2;
$num2 = $temp;

echo "After swapping: num1 = $num1, num2 = $num2\n";


}
function swapWithoutTemp($num1, $num2) {
echo "Before swapping: num1 = $num1, num2 = $num2\n";

$num1 = $num1 + $num2;


$num2 = $num1 - $num2;
$num1 = $num1 - $num2;

echo "After swapping: num1 = $num1, num2 = $num2\n";


}

$num1 = 10;
$num2 = 20;

echo "Swapping with temporary variable:\n";


swapWithTemp($num1, $num2);
echo "\nSwapping without temporary variable:\n";
swapWithoutTemp($num1, $num2);
?>
OUTPUT
Ques 9)Write a PHP program to add, subtract,
multiply &amp; divide two numbers using
functions.
CODE
<?php
// Function to add two numbers
function add($num1, $num2) {
return $num1 + $num2;
}

// Function to subtract two numbers


function subtract($num1, $num2) {
return $num1 - $num2;
}

// Function to multiply two numbers


function multiply($num1, $num2) {
return $num1 * $num2;
}

// Function to divide two numbers


function divide($num1, $num2) {
if ($num2 == 0) {
return "Error: Division by zero";
} else {
return $num1 / $num2;
}
}

// Test the functions


$num1 = 10;
$num2 = 5;

echo "Number 1: $num1\n";


echo "Number 2: $num2\n\n";

echo "Addition: " . add($num1, $num2) . "\n";


echo "Subtraction: " . subtract($num1, $num2) . "\n";
echo "Multiplication: " . multiply($num1, $num2) . "\n";
echo "Division: " . divide($num1, $num2) . "\n";
?>
OUTPUT
Ques 10)Write a program to show the usage of
the SESSION variable.
CODE
File 1:index.php

<?php
session_start();

if(isset($_SESSION['username'])) {
echo "Welcome back, " . $_SESSION['username'] . "! <br>";
echo "<a href='logout.php'>Logout</a>";
} else {
echo "Welcome, guest! <br>";
echo "<a href='login.php'>Login</a>";
}
?>
File 2:login.php

<?php
session_start();
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$username = $_POST['username'];
$_SESSION['username'] = $username;
header("Location: index.php");
exit;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Login</title>
</head>
<body>
<h2>Login</h2>
<form method="post" action="<?php echo
htmlspecialchars($_SERVER["PHP_SELF"]);?>">
Username: <input type="text"
name="username"><br><br>
<input type="submit" value="Login">
</form>
</body>
</html>
File 3:logout.php

<?php
session_start();
$_SESSION = array();
session_destroy();
header("Location: index.php");
exit;
?>

You might also like