PHP Practicals
PHP Practicals
<?php
for ($i = 1; $i <= 10; $i++) {
if ($i % 2 == 0) {
echo "$i is even\n";
} else {
echo "$i is odd\n";
}
}
?>
<?php
$rows = 5;
for ($i = 1; $i <= $rows; $i++) {
for ($j = $i; $j < $rows; $j++) {
echo " ";
}
$stars = 2 * $i - 1;
echo str_pad("", $stars, "*") . "\n";
}
?>
Practical4
<?php
$students = array(
"Alice" => array("Math" => 85, "Science" => 92, "English" => 88),
"Bob" => array("Math" => 78, "Science" => 81, "English" => 74),
);
echo "Student Marks Information:<br>";
foreach ($students as $studentName => $subjects) {
echo "<strong>Student: $studentName</strong><br>";
foreach ($subjects as $subject => $marks) {
echo "$subject: $marks<br>";
}
echo "<br>";
}
echo "<br>Alternatively, printing the multidimensional array using print_r():<br>";
print_r($students);
?>
Practical5
1. Write a PHP program to- i) Calculate length of string.ii)Count the number of words
in string without using string functions.
<?php
$input = "This is a sample string";
$length = 0;
for ($i = 0; isset($input[$i]); $i++) {
$length++; }
echo "Length of the string: $length<br>";
$wordCount = 0;
$inWord = false;
for ($i = 0; $i < $length; $i++) {
if ($input[$i] != ' ' && !$inWord) {
$inWord = true;
$wordCount++;
} elseif ($input[$i] == ' ') {
$inWord = false; }}
echo "Number of words in the string: $wordCount<br>";
?>
1.Write a program to design a registration form using textbox, radio button, checkbox
and button.
<html>
<head>
<title>Registration Form</title>
</head>
<body>
<h2>Registration Form</h2>
<form action="pr10a.php" method="POST">
Full Name: <input type="text" id="name" name="name" required><br>
Email:<input type="email" id="email" name="email" required><br>
Gender:<input type="radio" id="male" name="gender" value="Male" required>
male
<input type="radio" id="female" name="gender" value="Female"> female<br>
Interests:<br>
<input type="checkbox" id="coding" name="interests[]" value="Coding">Coding<br>
<input type="checkbox" id="design" name="interests[]"
value="Design">Design<br>
<input type="checkbox" id="music" name="interests[]" value="Music">Music<br>
<input type="submit" value="Register">
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = $_POST['name'];
$email = $_POST['email'];
$gender = $_POST['gender'];
$interests = isset($_POST['interests']) ? implode(", ", $_POST['interests']) :
"None";
echo "<h3>Registration Successful!</h3>";
echo "<p>Name: $name</p>";
echo "<p>Email: $email</p>";
echo "<p>Gender: $gender</p>";
echo "<p>Interests: $interests</p>";
}
?>
</body>
</html>
2.
<html >
<head>
<title>College Admission Form</title>
<script>
function validateForm() {
var name = document.getElementById("name").value;
var email = document.getElementById("email").value;
var phone = document.getElementById("phone").value;
var sscMarks = document.getElementById("sscMarks").value;
var errorMessages = "";
if (name.trim() == "") {
errorMessages += "Name is required.<br>";
}
var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6}$/;
if (!emailPattern.test(email)) {
errorMessages += "Please enter a valid email address.<br>";
}
var phonePattern = /^[0-9]{10}$/;
if (!phonePattern.test(phone)) {
errorMessages += "Phone number should be a valid 10-digit
number.<br>";
}
if (sscMarks < 0 || sscMarks > 100 || isNaN(sscMarks)) {
errorMessages += "SSC marks should be between 0 and 100.<br>";
}
if (errorMessages !== "") {
document.getElementById("errorMessages").innerHTML = errorMessages;
return false;
}
return true;
}
</script>
</head>
<body>
<h2>College Admission Form</h2>
<form action="pr10b.php" method="POST" onsubmit="return validateForm()">
Full Name:<input type="text" id="name" name="name" placeholder="Enter your
full name" required><br>
email:<input type="email" id="email" name="email" placeholder="Enter your
email" required><br>
Phone Number: <input type="text" id="phone" name="phone" placeholder="Enter
your 10-digit phone number" required><br>
SSC Marks (Percentage): <input type="text" id="sscMarks" name="sscMarks"
placeholder="Enter your SSC marks" required><br>
<input type="submit" value="Submit">
</form>
<div id="errorMessages" style="color: red;"></div>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$sscMarks = $_POST['sscMarks'];
$errorMessages = "";
if (empty($name)) {
$errorMessages .= "Name is required.<br>";
}
$emailPattern = "/^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6}$/";
if (!preg_match($emailPattern, $email)) {
$errorMessages .= "Please enter a valid email address.<br>";
}
$phonePattern = "/^[0-9]{10}$/";
if (!preg_match($phonePattern, $phone)) {
$errorMessages .= "Phone number should be a valid 10-digit number.<br>";
}
if ($sscMarks < 0 || $sscMarks > 100 || !is_numeric($sscMarks)) {
$errorMessages .= "SSC marks should be between 0 and 100.<br>";
}
if ($errorMessages != "") {
echo "<div style='color: red;'><b>Errors:</b><br>" . $errorMessages . "</div>";
} else {
echo "<h3>Admission Successful!</h3>";
echo "<p>Name: $name</p>";
echo "<p>Email: $email</p>";
echo "<p>Phone Number: $phone</p>";
echo "<p>SSC Marks: $sscMarks</p>";
}
}
?>
</body>
</html>
Practical11
1.Write a program to design a form using list box, combo box and Hidden field box.
<html><head></head><body>
<h2>Form with List Box, Combo Box, and Hidden Field</h2>
<form action="pr11a.php" method="POST">
<label for="comboBox">Select a City:</label>
<select name="comboBox" id="comboBox">
<option value="Mumbai">Mumbai</option>
<option value="Pune">Pune</option>
<option value="Delhi">Delhi</option>
<option value="Chennai">Chennai</option></select><br><br>
<label for="listBox">Select your Hobbies (multiple):</label>
<select name="hobbies[]" id="listBox" multiple>
<option value="Reading">Reading</option>
<option value="Traveling">Traveling</option>
<option value="Cooking">Cooking</option>
<option value="Sports">Sports</option></select><br><br>
<input type="hidden" name="userId" value="12345">
<input type="submit" value="Submit"></form>
<?php if ($_SERVER["REQUEST_METHOD"] == "POST") {
$selectedCity = $_POST['comboBox'];
$hobbies = $_POST['hobbies'];
$userId = $_POST['userId'];
echo "<h2>Form Submitted Successfully!</h2>";
echo "User ID: $userId<br>";
echo "Selected City: $selectedCity<br>";
echo "Selected Hobbies: " . implode(", ", $hobbies) . "<br>";} ?>
</body></html>
2.Write a program to design a form using list box, combo box.
<html><head></head><body>
<h2>Form with List Box and Combo Box</h2>
<form action="pr11b.php" method="POST">
<label for="comboBox">Select a Country:</label>
<select name="comboBox" id="comboBox">
<option value="India">India</option>
<option value="USA">USA</option>
<option value="Australia">Australia</option>
<option value="Canada">Canada</option></select><br><br>
<label for="listBox">Select your Hobbies (multiple):</label>
<select name="hobbies[]" id="listBox" multiple>
<option value="Reading">Reading</option>
<option value="Traveling">Traveling</option>
<option value="Cooking">Cooking</option>
<option value="Sports">Sports</option> </select> <br><br>
<input type="submit" value="Submit"></form>
<?phpif ($_SERVER["REQUEST_METHOD"] == "POST") {
$selectedCountry = $_POST['comboBox'];
$selectedHobbies = $_POST['hobbies'];
echo "<h2>Form Submitted Successfully!</h2>";
echo "Selected Country: $selectedCountry<br>";
echo "Selected Hobbies: ";
if (!empty($selectedHobbies)) {
echo implode(", ", $selectedHobbies);
} else {
echo "No hobbies selected.";
}
}
?>
</body>
</html>
Practical12
<html>
<head>
</head>
<body>
<h2>Email Validation Form</h2>
<form method="POST">
<label for="email">Enter your email:</label>
<input type="text" id="email" name="email" required>
<input type="submit" value="Submit">
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$email = $_POST['email'];
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
echo "<p style='color:green;'>The email address '$email' is valid!</p>";
} else {
echo "<p style='color:red;'>The email address '$email' is not valid!</p>";
}
}
?>
</body>
</html>
Practical13