KTU Web Programming QPs
KTU Web Programming QPs
1. prime.php
<?php
function isPrime($number){
if ($number<2) {
return false;
}
Output:
Output: Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 )
● rsort():Sorts an array in descending order. The keys are re-indexed.
Syntax:
$arr = [3, 1, 4, 2];
rsort($arr);
print_r($arr); \
Output: Array ( [0] => 4 [1] => 3 [2] => 2 [3] => 1 )
3.(a)
String handling functions in PHP are:
● strlen( ):Returns the length of a string.
Example:
$string = "Hello, World!";
$length = strlen($string);
echo "Length of the string: $length";
Output: Length of the string: 13
● strpos( ):Finds the position of the first occurrence of a substring within a string. Returns
false if the substring is not found.
Example:
$string = "Hello, World!";
$position = strpos($string, "World");
echo "Position of 'World': $position";
Output: Position of 'World': 7
● substr( ):Returns a portion of a string specified by the starting position and length.
Example:
$string = "Hello, World!";
$substring = substr($string, 7, 5);
echo "Substring: $substring";
Output: Substring: World
● trim( ):Removes whitespace (or other specified characters) from the beginning and end
of a string.
Example:
$string = " Hello, World! ";
$trimmedString = trim($string);
echo "Trimmed string: '$trimmedString'";
Output: Trimmed string: 'Hello, World!'
3. (b)
<!DOCTYPE html>
<html>
<head>
<title>Calculator Form</title>
</head>
<body>
<?php
if (isset($_GET['num1']) && isset($_GET['num2'])) {
// Collect the data from the form
$num1 = floatval($_GET['num1']);
$num2 = floatval($_GET['num2']);
</body>
</html>
4(a). sum.php
<?php
$a = 1;
$sum = 0;
do {
$sum += $a;
$a++;
} while ($a <= 100);
echo "Sum is $sum\n";
?>
● array_pop( ):
Removes the last element from an array and returns it.
Example:
$fruits = ["Apple", "Banana", "Cherry"];
$lastFruit = array_pop($fruits);
echo $lastFruit;
Output: Cherry
print_r($fruits);
Output: Array ( [0] => Apple [1] => Banana )
● array_merge( ):
Used to merge one or more arrays into a single array.
Example:
<?php
$array1 = ['apple', 'banana'];
$array2 = ['orange', 'grape'];
$array3 = ['kiwi' => 'green', 'melon' => 'yellow'];
$mergedArray = array_merge($array1, $array2, $array3);
print_r($mergedArray);
?>
● in_array( ):
Checks if a value exists in an array.
Example:
$colors = ["red", "green", "blue"];
$exists = in_array("green", $colors);
echo $exists ? "Green is in the array." : "Green is not in the array.";
Output: Green is in the array.
5. Array.php
<?php
$students = [
"roll3" => "Frank",
"roll1" => "Alice",
"roll5" => "Eve",
"roll0" => "Jack",
"roll2" => "Bob",
"roll9" => "Hannah",
"roll6" => "David",
"roll4" => "Grace",
"roll8" => "Charlie",
"roll7" => "Ivy"
];
echo "Original Array:\n";
foreach ($students as $roll => $name) {
echo "$roll: $name\n";
}
$students1 = $students;
asort($students1);
echo "\nArray Sorted by Names (asort):\n";
foreach ($students1 as $roll => $name) {
echo "$roll: $name\n";
}
$students2 = $students;
ksort($students2);
echo "\nArray Sorted by Roll Numbers (ksort):\n";
foreach ($students2 as $roll => $name) {
echo "$roll: $name\n";
}
$students3 = $students;
rsort($students3);
echo "\nArray Sorted by Names in Descending Order (rsort):\n";
foreach ($students3 as $name) {
echo "$name\n";
}
?>
Output:
Original Array:
roll3: Frank
roll1: Alice
roll5: Eve
roll0: Jack
roll2: Bob
roll9: Hannah
roll6: David
roll4: Grace
roll8: Charlie
roll7: Ivy
6. oddoreven.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<form action="even.php" method="post">
<input type="text" name="number" placeholder="Enter the number">
<input type="submit" name="" id="" value="Submit">
</form>
<?php
$number = (int) $_POST["number"];
if ($number) {
if ($number % 2 == 0) {
echo "<p>The entered number is Even</p><br>";
} else {
echo "<p>The entered number is Odd</p><br>";
}
}
else {
echo "<p>Enter the Number</p><br>";
}
?>
</body>
</html>
7. <?php
$ages = [
"Alice" => 30,
"Bob" => 30,
"Harry" => 35,
"Mary" => 32
];
$ages["Mary"] = 28;
asort($ages);
echo "Sorted Ages:\n";
foreach ($ages as $name => $age) {
echo "$name: $age\n";
}
$bobAge = $ages["Bob"];
echo "\nAge of Bob: $bobAge\n";
?>