Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
3 views

KTU Web Programming QPs

The document contains various PHP code snippets demonstrating functions for checking prime numbers, sorting arrays, string handling, and creating forms for user input. It also includes examples of array manipulation and sorting, as well as a simple calculator and odd/even number checker. Additionally, it showcases how to manage associative arrays and output results based on user input.

Uploaded by

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

KTU Web Programming QPs

The document contains various PHP code snippets demonstrating functions for checking prime numbers, sorting arrays, string handling, and creating forms for user input. It also includes examples of array manipulation and sorting, as well as a simple calculator and odd/even number checker. Additionally, it showcases how to manage associative arrays and output results based on user input.

Uploaded by

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

Group - 1

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

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


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

$number=intval(readline("Enter the number :"));


if(isPrime($number)){
echo "$number is Prime\n";
}
else{
echo "$number is Not Prime\n";
}
?>

Output:

Enter the number :1


1 is Not Prime

Enter the number :23


23 is Prime

Enter the number :2


2 is Prime

2. The various sorting functions for arrays in PHP are:


● sort():Sorts an array in ascending order. The keys are re-indexed.
Syntax:
$arr = [3, 1, 4, 2];
sort($arr);
print($arr);

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 )

● asort():Sorts an associative array in ascending order, maintaining key/value


associations.
Syntax:
$arr = ["b" => 3, "a" => 1, "c" => 2];
asort($arr);
print_r($arr);
Output: Array ( [a] => 1 [c] => 2 [b] => 3 )

● arsort():Sorts an associative array in descending order, maintaining key/value


associations.
Syntax:
$arr = ["b" => 3, "a" => 1, "c" => 2];
arsort($arr);
print_r($arr);
Output: Array ( [b] => 3 [c] => 2 [a] => 1 )

● ksort():Sorts an associative array by key in ascending order.


Syntax:
$arr = ["b" => 3, "a" => 1, "c" => 2];
ksort($arr);
print_r($arr);
Output: Array ( [a] => 1 [b] => 3 [c] => 2 )

● krsort():Sorts an associative array by key in descending order.


Syntax:
$arr = ["b" => 3, "a" => 1, "c" => 2];
krsort($arr);
print_r($arr);
Output: Array ( [c] => 2 [b] => 3 [a] => 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

● str_replace( ):Replaces all occurrences of a search string with a replacement string.


Example:
$string = "Hello, World!";
$newString = str_replace("World", "PHP", $string);
echo $newString;
Output: Hello, PHP!

● strtoupper( ):Converts all characters in a string to uppercase.


Example:
$string = "Hello, World!";
$upperString = strtoupper($string);
echo $upperString;
Output: HELLO, 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>

<form action="" method="GET">


<label for="num1">Number 1:</label>
<input type="text" id="num1" name="num1" required>
<br><br>

<label for="num2">Number 2:</label>


<input type="text" id="num2" name="num2" required>
<br><br>

<input type="submit" value="Calculate">


</form>

<?php
if (isset($_GET['num1']) && isset($_GET['num2'])) {
// Collect the data from the form
$num1 = floatval($_GET['num1']);
$num2 = floatval($_GET['num2']);

// Calculate sum, difference, and product


$sum = $num1 + $num2;
$difference = $num1 - $num2;
$product = $num1 * $num2;

// Display the results


echo "<h3>Results:</h3>";
echo "Sum: $sum<br>";
echo "Difference: $difference<br>";
echo "Product: $product<br>";
}
?>

</body>
</html>

4(a). sum.php
<?php
$a = 1;
$sum = 0;
do {
$sum += $a;
$a++;
} while ($a <= 100);
echo "Sum is $sum\n";
?>

Output: Sum is 5050

4(b). Various ways to create an array in php are:


● Using array( ):
$fruits = array("Apple", "Banana", "Cherry");

● Using the shorthand [ ] syntax:


$vegetables = ["Carrot", "Potato", "Onion"];

● Creative an associative array:


$ages = array("Alice" => 30, "Bob" => 25, "Charlie" => 35);

● Using the shorthand [ ] associative array:


$scores = ["Alice" => 90, "Bob" => 85, "Charlie" => 88];

● Creating a multidimensional array:


$students = [
["name" => "Alice", "age" => 20],
["name" => "Bob", "age" => 22]
];

Four functions that deal with php array are:


● array_push( ):
Adds one or more elements to the end of an array.
Example:
$numbers = [1, 2, 3];
array_push($numbers, 4, 5);
print_r($numbers);
Output: Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 )

● 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

Array Sorted by Names (asort):


roll1: Alice
roll2: Bob
roll8: Charlie
roll6: David
roll5: Eve
roll3: Frank
roll4: Grace
roll9: Hannah
roll7: Ivy
roll0: Jack

Array Sorted by Roll Numbers (ksort):


roll0: Jack
roll1: Alice
roll2: Bob
roll3: Frank
roll4: Grace
roll5: Eve
roll6: David
roll7: Ivy
roll8: Charlie
roll9: Hannah

Array Sorted by Names in Descending Order (rsort):


Jack
Ivy
Hannah
Grace
Frank
Eve
David
Charlie
Bob
Alice

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

You might also like