String Set-1 - : Code
String Set-1 - : Code
Lab 15
----------------String Set-1-----------------
Code:
<!DOCTYPE html>
<html>
<head>
<title>Set1</title>
</head>
<body>
<h1>Set 1 Questions</h1>
<?php
//Q1
echo "<b>Write a PHP script to split the following string.</b><br>";
$str = "06052025";
$new_str = substr($str, 0, 2) . '-' . substr($str, 2, 2) . '-' . substr($str, 4, 4);
echo "Date: ".$new_str;
//Q2
echo "<br><br>";
echo "<b>Write a PHP script to extract the user name from the following email
ID.</b><br>";
$email = "[email protected]";
$Pos = strpos($email, "@");
$username = substr($email, 0, $Pos);
echo "Username: " . $username;
//Q3
echo "<br><br>";
echo "<b>Write a PHP script to check whether a string contains a specific string? Check
whether the above string contains the string 'university'.</b><br>";
$str = "I am in Adani University";
$lowerstr = strtolower($str);
$target = "university";
echo "String is: ";
echo "$str";
echo "<br>";
if (strpos($lowerstr, $target) == true) {
echo "string contains 'university'";
} else {
echo "string does not contain 'university'.";
}
//Q.4
echo "<br><br>";
echo "<b>Write a PHP script to find the first character that is different between two
strings. str1: 'bad' str2: 'bed'.</b><br>";
$str1 = "bad";
$str2 = "bed";
$length1 = strlen($str1);
$length2 = strlen($str2);
if ($length1 < $length2) {
$limit = $length1;
} else {
$limit = $length2;
}
$found = false;
for ($i = 0; $i < $limit; $i++) {
if ($str1[$i] !== $str2[$i]) {
echo "First difference at index $i: '{$str1[$i]}' vs '{$str2[$i]}'";
$found = true;
break;
}
}
if (!$found) {
if ($length1 != $length2) {
echo "Strings are equal up to $limit characters, but lengths differ.";
} else {
echo "The strings are identical.";
}
}
?>
</body>
</html>
Output:
Lab 16
----------------String Set-2-----------------
Q1. Write a PHP script to get the absolute difference
between x and 50 . If X is greater than 50, then print 4
times the absolute difference.
Q2. Write a PHP script to remove the character from the
given position from a string.
Q3. Interchange first and last digits of a given number.
Q4. Write a PHP script to insert a character at a
specified position.
Q5. Write a PHP script to Find and Replace.
Code:
<!DOCTYPE html>
<html>
<head>
<title>Set2</title>
</head>
<body>
<h1>Set 2 PHP Questions</h1>
<?php
//Q1
echo "<b> Write a php program to get the abosolute difference x and 50. If x is greater than 50
return triple the absolute difference.</b><br>";
$x = 57;
$result = abs($x - 50);
if ($x > 50) {
$result = 3 * $result;
}
echo "The absolute difference is: $result<br>";
//Q2
echo "<br><b>Write a PHP script to remove the character from the given position from a
string.</b><br>";
$s1 = "Hello";
$i = 2;
if ($i > strlen($s1)) {
echo "Index invalid<br>";
}
else {
$result1 = substr($s1, 0, $i).substr($s1, $i+1, strlen($s1)-$i);
echo "Original String: $s1<br>";
echo "Modified String: $result1<br>";
}
// Q.3
echo "<br><b>Interchange first and last digits of a given number.</b><br>";
$number2 = 12345;
$numStr = (String)$number2;
$length = strlen($numStr);
if ($length < 2) {
echo "Result: " . $number;
} else {
$first = $numStr[0];
$last = $numStr[$length - 1];
$numStr[0] = $last;
$numStr[$length - 1] = $first;
//Q4
echo "<br><br><b>Write a PHP script to insert a character at a specified position.</b><br>";
$s2 = "JavaScript";
$ch = "b";
$pos = 3;
if ($pos > strlen($s2)) {
echo "Index invalid<br>";
}
$result2 = substr($s2, 0, $pos).$ch.substr($s2, $pos, strlen($s2)-$pos);
echo "Original String: $s2<br>";
echo "Modified String: $result2<br>";
//Q5
echo "<br><b>Write a PHP script to Find and Replace.</b><br>";
$string4 = "Hello World!";
$find = "World";
$replace = "PHP";
$position = strpos($string4, $find);
if ($position !== false) {
$newString = substr($string4, 0, $position) . $replace . substr($string4, $position +
strlen($find));
echo "Original String: $string4\n";
echo "<br>Modified String: $newString\n";
} else {
echo "Substring not found.";
}
?>
</body>
</html>
Output:
Lab 17
---------------- LOOPS-----------------
Q1. Write a PHP program to compute the sum of the two
reversed numbers and display the sum in reversed form.
Input: 13,14
Output: 72
Q2. Write a PHP program which iterates the integers
from 1 to 30. For multiples of three print "three" instead
of the number and for the multiples of five print "five".
For numbers which are multiples of both three and five
print "threefive".
Q3. Write a PHP program to check two given integers,
each in the range 10..99. Return true if a digit appears
in both numbers, such as the 5 in 15 and 51.
Q4. Check that the given n digit number is palindrome
or not (Without Using PHP Predefine Function).
Q5. PHP script for generating a list of prime numbers
below 100.
Q6. Reversing Number
Q7. Write a PHP program to reverse the digits of an
integer.
Q8. Write a PHP script using nested for loop that creates
a chess board as shown below.Use table width="270px"
and take 30px as cell height and width.
Q9. Write a PHP script to create multiplication table
from 1 to 10 with cell spacing 5px and cellpadding 5px.
Code:
<!DOCTYPE html>
<html>
<head>
<title>Set3</title>
</head>
<body>
<h1>Set 3 PHP Questions</h1>
<?php
// Q.1
echo "<b>Write a PHP program to compute the sum of the two reversed numbers and
display the sum in reversed form.</b><br>";
$num1 = 13;
$num2 = 14;
$rev1 = (int)strrev((string)$num1);
$rev2 = (int)strrev((string)$num2);
$sum = $rev1 + $rev2;
$result = strrev((string)$sum);
// Q.2
echo "<b>Write a PHP program which iterates the integer from 1 to 30. For multiples of
three print 'three' instead of the number and for the multiples of five print 'five'. For numbers
which are multiples of both three and five print 'threefive'.</b><br>";
for ($i = 1; $i <= 30; $i++) {
if ($i % 3 == 0 && $i % 5 == 0) {
echo "threefive ";
} elseif ($i % 3 == 0) {
echo "three ";
} elseif ($i % 5 == 0) {
echo "five ";
} else {
echo $i . " ";
}
}
// Q.3
echo "<br><br><b>Write a PHP program to check two given integers, each in the range
10to99. Return true if a digit appears in both numbers, such as the 5 in 15 and 51.</b><br>";
$x = "15";
$y = "51";
$n = "5";
if (($x[0] == $n || $x[1] == $n) && ($y[0] == $n || $y[1] == $n)) {
echo "The digit appears in both the numbers<br";
}
else {
echo "The digit does not appear in both the numbers<br>";
}
// Q.4
echo "<br><br><br><b>Check that the given n digit number is palindrome or
not</b><br>";
$num = 121;
echo "Number is $num <br>";
$str = (string) $num;
$numlen = strlen($str);
$isPalindrome = true;
// Q.5
echo "<br><b>PHP script for generating a list of prime numbers below 100.</b><br>";
for ($i = 2; $i < 100; $i++) {
$isPrime = true;
for ($j = 2; $j <= sqrt($i); $j++) {
if ($i % $j == 0) {
$isPrime = false;
break;
}
}
if ($isPrime) {
echo "$i ";
}
}
// Q.6
echo "<br><br><br><b>Write a php program Reversing Number</b><br>";
$num = 12345;
$rev = strrev((string) $num);
//Q.7
echo "<br><br><b>Write a PHP program to reverse the digits of an integer.</b><br>";
$num = 12345;
$rev = 0;
while ($num >0){
$x = $num % 10;
$rev = $rev * 10 + $x;
$num = (int)($num / 10);
}
// Q.8
echo "<br><b>Write a PHP script using nested for loop that creates a chess board as
shown below.Use table width=270px and take 30px as cell height and width.</b><br>";
// Q.9
echo "<br><b>Write a PHP script to create multiplication table from 1 to 10 with cell
spacing 5px and cellpadding 5px.</b><br>";
echo "<table cellspacing='5' cellpadding='5'>";
for ( $i = 1; $i <= 10; $i++) {
echo "<tr>";
for ($j = 1; $j <= 10; $j++) {
echo "<td>" .$i. "*". $j . "=" .($i * $j) . "</td>";
}
echo "</tr>";
}
echo "</table>";
?>
</body>
</html>
Output:
Lab 17
---------------- Functions-----------------
Q1. Write a PHP function that checks whether a string is
all lowercase. (Ord () gives ASCII value of the character )
Q2. Write a PHP function that checks whether a passed
string is a palindrome or not ?
Q3. Write a PHP function to add the digits of a positive
integer repeatedly until the result has a single digit. For
example given number is 59 , the result is 5.
Step1: 5 + 9 = 14
Step 2 : 1 + 4 = 5
Input : 48 Output : 3
Code:
<!DOCTYPE html>
<html>
<head>
<title>Set4</title>
</head>
<body>
<h1>Set 4 PHP Questions</h1>
<?php
// Q.1
echo "<b>Write a php fucntion to check whether the string is all lowercase</b><br>";
function islowercase($s) {
$flag = true;
for ($i = 0; $i < strlen($s); $i++) {
if (ord($s[$i]) >= 65 && ord($s[$i]) <= 90) {
$flag = false;
}
}
if ($flag) {
echo "$s is Contain all lowercase<br>";
}
else {
echo "$s is not Contain all lowercase<br>";
}
}
islowercase("Yash");
islowercase("yash");
// Q.2
if ($revstr == $str) {
echo "$str is a palindrome<br>";
}
else {
echo "$str is not a palindrome<br>";
}
}
strPalindrome("Jalaj");
strPalindrome("Yash");
// Write a php function to add the digits of a positive integer repeatedly until the result
has a single digit
echo "<br><br><b>Write a PHP function to add the digits of a positive integer
repeatedly until the result has a single digit. For example given number is 59 , the result is
5.</b><br>";
function sumdigits($num) {
if ($num <= 0) {
echo "The number should be greater than 0<br>";
return;
}
else {
$sum = 0;
while ($num > 0) {
$rem = $num % 10;
$sum = $sum + $rem;
$num = (int) ($num /10);
}
if ($sum > 9) {
return sumdigits($sum);
}
else {
return $sum;
}
}
}
echo "The sum of number until single digit: ".sumdigits(59)."<br>";
echo "The sum of number until single digit: ".sumdigits(48)."<br>";
?>
</body>
</html>
Output:
Lab 18
---------------- Array-----------------
Q1. Write a PHP function to find three numbers
from an array such that the sum of three
consecutive numbers equal to zero.
Code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Array</title>
</head>
<body>
<?php
//Q.1
echo "<b>Write a PHP function to find three numbers from an array such that the sum
of three consecutive numbers equal to zero.</b><br>";
function Sum($arr) {
$n = count($arr);
for ($i = 0; $i <= $n - 3; $i++) {
$sum = $arr[$i] + $arr[$i+1] + $arr[$i+2];
if ($sum == 0) {
echo "Three consecutive numbers with sum 0 are: {$arr[$i]}, {$arr[$i+1]},
{$arr[$i+2]}<br>";
return;
}
}
echo "No three consecutive numbers found with sum 0.";
}
$arr = [4, -3, -1, 2, -2, 0, 1];
Sum($arr);
//Q.2
echo "<br><b>Write a PHP program to find a single number in an array that doesn't
occur twice.</b><br>";
function single($arr) {
$arr2 = [];
$n = count($arr);
Lab 19
---------------- Form and database connection
-----------------
Q1. Make a form and submit the data to the connected sql
database and then display the Data.
Code:
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$host = "localhost";
$user = "root";
$pass = "";
$db = "practical";
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$conn->close();
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Registration Form in Html css </title>
<style>
body { margin-top: 5px;
font-family: Arial, sans-serif; border: 1px solid #ccc;
background-color: #37279f; border-radius: 5px;
display: flex; }
justify-content: center;
align-items: center; .gender,
height: 100vh; .hobby {
margin: 0; display: flex;
} /* border: 1px solid black; */
width: 200px;
.container { align-items: center;
background: #ffffff; margin-left: 30px;
padding: 20px; }
border-radius: 10px;
box-shadow: 0px 0px 10px rgba(0, .hobby {
0, 0, 0.1); width: 300px;
width: 400px; }
}
.hobby label {
h2 { font-weight: normal;
text-align: center; }
color: #333;
} button {
width: 100%;
label { padding: 10px;
display: block; margin-top: 15px;
margin-top: 10px; border: none;
} border-radius: 5px;
background: #285ba7;
input, color: white;
select { font-size: 16px;
width: 100%; cursor: pointer;
padding: 8px; }
</style>
</head>
<body>
<div class="container">
<h2>Registration Form</h2>
<form action="#" method="post">
<label for="name">Name</label>
<input type="text" id="name" name="name" required>
<label for="email">Email</label>
<label>Gender</label>
<div class="gender">
<input type="radio" id="male" name="gender" value="male">
<label for="male">Male</label>
<input type="radio" id="female" name="gender" value="female">
<label for="female">Female</label>
</div>
<label for="city">City</label>
<select id="city" name="city" required>
<option value="">Select City</option>
<option value="delhi" selected>Delhi</option>
<option value="mumbai">Mumbai</option>
<option value="bangalore">Bangalore</option>
<option value="chennai">Chennai</option>
</select>
<button type="submit">Register</button>
</form>
</div>
</body>
</html>
Output: