KEMBAR78
Web Tech Pract 3 | PDF | Php | Computer Programming
0% found this document useful (0 votes)
41 views8 pages

Web Tech Pract 3

The document contains multiple PHP scripts and HTML forms for various string and arithmetic operations. It includes functionalities such as checking string positions, splitting strings, performing arithmetic calculations, and creating a chess board using CSS. Additionally, it demonstrates the use of Bootstrap for styling buttons and layouts.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views8 pages

Web Tech Pract 3

The document contains multiple PHP scripts and HTML forms for various string and arithmetic operations. It includes functionalities such as checking string positions, splitting strings, performing arithmetic calculations, and creating a chess board using CSS. Additionally, it demonstrates the use of Bootstrap for styling buttons and layouts.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

1) Design an HTML form to accept two strings from the user. Write a PHP script for the following.

a. Find whether the small string appears at the start of the large string.
b. Find the position of the small string in the big string.
c. Compare both the string for first n characters, also the comparison should not be case sensitive.
<!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 method="POST" action="1.php">

<label for="smallString">Small String:</label>

<input type="text" name="smallString" id="smallString" required>

<label for="bigString">Big String:</label>

<input type="text" name="bigString" id="bigString" required>

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

</form>

</body>

</html>

<?php

if ($_SERVER["REQUEST_METHOD"] == "POST") {

$smallString = $_POST["smallString"];

$bigString = $_POST["bigString"];

// a. Find whether the small string appears at the start of the large string

$startsWith = (strpos($bigString, $smallString) === 0);


// b. Find the position of the small string in the big string

$position = strpos($bigString, $smallString);

// c. Compare both strings for the first n characters (case insensitive)

$n = 5; // Change this value to the desired number of characters to compare

$comparison = strncasecmp($smallString, $bigString, $n);

// Output the results

echo "Starts with small string: " . ($startsWith ? "Yes" : "No") . "<br>";

echo "Position of small string: " . ($position !== false ? $position : "Not found") . "<br>";

echo "Comparison of first $n characters: " . ($comparison == 0 ? "Equal" : "Not equal") . "<br>";

?>

2)Write a PHP script for the following: Design a form having a text box and a drop down list
containing any 3 separators(e.g. #, |, %, @, ! or comma) accept a strings from the user and also a
separator.
a. Split the string into separate words using the given separator.
b. Replace all the occurrences of separator in the given string with some other separator.
c. Find the last word in the given string.
<!DOCTYPE html>
<html>
<head>
<title>String Separator Operations</title>
</head>
<body>
<h2>String Separator Operations</h2>
<form method="post">
<label for="inputString">Enter a String:</label><br>
<input type="text" name="inputString" id="inputString" required><br><br>

<label for="separator">Choose a Separator:</label><br>


<select name="separator" id="separator" required>
<option value="#">#</option>
<option value="|">|</option>
<option value="%">%</option>
</select><br><br>

<label for="newSeparator">Replace With Separator:</label><br>


<select name="newSeparator" id="newSeparator" required>
<option value="@">@</option>
<option value="!">!</option>
<option value=",">,</option>
</select><br><br>

<input type="submit" name="submit" value="Process">


</form>

<?php
if (isset($_POST['submit'])) {
$inputString = $_POST['inputString'];
$separator = $_POST['separator'];
$newSeparator = $_POST['newSeparator'];

echo "<h3>Results:</h3>";

// (a) Split the string using the chosen separator


$words = explode($separator, $inputString);
echo "<b>Split Words:</b><br>";
foreach ($words as $index => $word) {
echo "Word " . ($index + 1) . ": " . htmlspecialchars($word) . "<br>";
}

// (b) Replace all occurrences of separator with new separator


$replacedString = str_replace($separator, $newSeparator, $inputString);
echo "<br><b>String After Replacing Separator:</b><br>";
echo htmlspecialchars($replacedString) . "<br>";

// (c) Find the last word in the string


$lastWord = end($words);
echo "<br><b>Last Word:</b><br>";
echo htmlspecialchars($lastWord);
}
?>
</body>
</html>

3) Write a script to accept two integers(Use html form having 2 textboxes).


Write a PHP script to,
a. Find mod of the two numbers.
b. Find the power of first number raised to the second.
c. Find the sum of first n numbers (considering first number as n)
d. Find the factorial of second number.
(Write separate function for each of the above operations.)
<!DOCTYPE html>
<html>
<head>
<title>Integer Operations</title>
</head>
<body>
<form action="3.php" method="post">
<label for="num1">Enter first integer (n):</label><br>
<input type="number" id="num1" name="num1" required><br><br>

<label for="num2">Enter second integer:</label><br>


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

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


</form>
</body>
</html>

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$num1 = (int)$_POST['num1'];
$num2 = (int)$_POST['num2'];

// a. Find mod of the two numbers


function findModulus($a, $b) {
if ($b == 0) {
return "Cannot find modulus with zero as divisor.";
}
return $a % $b;
}

// b. Find the power of first number raised to the second


function findPower($base, $exponent) {
return pow($base, $exponent);
}

// c. Find the sum of first n numbers (considering first number as n)


function findSumOfNNumbers($n) {
if ($n < 0) {
return "Cannot find sum of negative numbers.";
}
return ($n * ($n + 1)) / 2;
}

// d. Find the factorial of second number


function findFactorial($num) {
if ($num < 0) {
return "Factorial is not defined for negative numbers.";
}
if ($num == 0) {
return 1;
}
$factorial = 1;
for ($i = 1; $i <= $num; $i++) {
$factorial *= $i;
}
return $factorial;
}

echo "<h2>Results:</h2>";
echo "Modulus of $num1 and $num2: " . findModulus($num1, $num2) . "<br>";
echo "Power of $num1 raised to $num2: " . findPower($num1, $num2) . "<br>";
echo "Sum of first $num1 numbers: " . findSumOfNNumbers($num1) . "<br>";
echo "Factorial of $num2: " . findFactorial($num2) . "<br>";
}
?>

4)Write a PHP script for the following: Design a form to accept two numbers from the user. Give
options to choose the arithmetic operation (use radio buttons). Display the result on the next
form. (Use the concept of function and default parameters. Use ‘include’ construct or require
statement)
<!DOCTYPE html>
<html>
<head>
<title>Arithmetic Calculator</title>
</head>
<body>
<h2>Enter Numbers and Choose Operation</h2>
<form action="calculator_result.php" method="post">
<label for="num1">First Number:</label>
<input type="number" id="num1" name="num1" required><br><br>

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


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

<label>Operation:</label><br>
<input type="radio" id="add" name="operation" value="add" checked>
<label for="add">Addition</label><br>

<input type="radio" id="subtract" name="operation" value="subtract">


<label for="subtract">Subtraction</label><br>

<input type="radio" id="multiply" name="operation" value="multiply">


<label for="multiply">Multiplication</label><br>

<input type="radio" id="divide" name="operation" value="divide">


<label for="divide">Division</label><br><br>

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


</form>
</body>
</html>
<?php
function performOperation($num1, $num2, $operation = 'add') {
switch ($operation) {
case 'add':
return $num1 + $num2;
case 'subtract':
return $num1 - $num2;
case 'multiply':
return $num1 * $num2;
case 'divide':
if ($num2 != 0) {
return $num1 / $num2;
} else {
return "Error: Division by zero!";
}
default:
return "Invalid operation.";
}
}
?>
Q1)) Create a button with different style (Secondary, Primary, Success, Error, Info, Warning, Danger)
using BootStrap.

Ans
<?php
$n1 = $_GET['a'];
$n2 = $_GET['b'];
$ch = $_GET['c'];

if($ch==1)
{
$c = $n1 + $n2;
echo"addition is: $c";
}
else if($ch==2)
{
$c = $n1 - $n2;
echo"subtraction is: $c";
}
else if($ch==3)
{
$c = $n1 * $n2;
echo"multiplication is: $c";
}
else if($ch==4)
{
$c = $n1 / $n2;
echo"Division is: $c";

}
?>

5) Create a button with different style (Secondary, Primary, Success, Error, Info, Warning, Danger)
using BootStrap.
<button type="button" class="btn btn-primary">Primary Button</button>
<button type="button" class="btn btn-secondary">Secondary Button</button>
<button type="button" class="btn btn-success">Success Button</button>
<button type="button" class="btn btn-info">Info Button</button>
<button type="button" class="btn btn-warning">Warning Button</button>
<button type="button" class="btn btn-danger">Danger Button</button>

6)Write a PHP script to create a chess board using CSS on table cells. [
<!DOCTYPE html>
<html>
<body>
<table width="400px" border="1px" cellspacing="0px">
<?php
echo "Chess by GeeksforGeeks";
$value = 0;

for($col = 0; $col < 8; $col++) {


echo "<tr>";
$value = $col;

for($row = 0; $row < 8; $row++) {


if($value%2 == 0) {
echo
"<td height=40px width=20px bgcolor=black></td>";
$value++;
}
else {
echo
"<td height=40px width=20px bgcolor=white></td>";
$value++;
}
}
echo "</tr>";
}
?>
</table>
</body>
</html>

7) Create a container add row inside it and add 3 columns inside row using BootStrap.
<div class="container">
<div class="row">
<div class="col-sm-4">
<div>Column 1: Row 1</div>
<div>Column 1: Row 2</div>
</div>
<div class="col-sm-4">
<div>Column 2: Row 1</div>
<div>Column 2: Row 2</div>
</div>
<div class="col-sm-4">
<div>Column 3: Row 1</div>
<div>Column 3: Row 2</div>
</div>
</div>
</div>

You might also like