Tutorial PHP and JS
Tutorial PHP and JS
Tutorial PHP and JS
<?php
function isPrime($num) {
if ($num <= 1) return false;
for ($i = 2; $i <= sqrt($num); $i++) {
if ($num % $i == 0) return false;
}
return true;
}
<?php
function isPalindrome($num) {
return strval($num) === strrev(strval($num));
}
<?php
function reverseNumber($num) {
return strrev(strval($num));
?>
4. List the different ways to create an array in PHP with an example.
<?php
print_r($array1);
print_r($array2);
print_r($array3);
print_r($array4);
?>
5. Write a PHP program to compute the sum of the positive integers up to 100 using
do while.
<?php
$sum = 0;
$i = 1;
do {
$sum += $i;
$i++;
} while ($i <= 100);
echo "Sum using do-while: $sum";
?>
6. Write a PHP program to compute the sum of the positive integers up to 100 using
a 3 for loop.
<?php
$sum = 0;
for ($i = 1; $i <= 100; $i++) {
$sum += $i;
}
echo "Sum using for loop: $sum";
?>
7. Write a PHP program to compute the sum of the positive integers up to 100 using
a while loop.
<?php
$sum = 0;
$i = 1;
while ($i <= 100) {
$sum += $i;
$i++;
}
echo "Sum using while loop: $sum";
?>
<?php
function sumOfDigits($num) {
$sum = 0;
while ($num != 0) {
return $sum;
?>
9. Write a PHP program to check whether the given number is Armstrong or not.
<?php
function isArmstrong($num) {
$sum = 0;
$temp = $num;
while ($temp != 0) {
$sum += $digit ** 3;
?>
10. Write a PHP code to display a message indicating whether the number is odd or
<!DOCTYPE html>
<html>
<body>
<form method="post">
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$number = $_POST['number'];
?>
</body>
</html>
11. List out the sorting functions for Arrays in PHP. Illustrate with suitable examples.
<?php
$array = array(4, 2, 8, 6);
sort($array); // Ascending order sort
echo "Sorted in ascending order: ";
print_r($array);
<?php
function factorial($num) {
$fact = 1;
for ($i = 1; $i <= $num; $i++) {
$fact *= $i;
}
return $fact;
}
13. Describe how input from an HTML form is retrieved in a PHP program, with an
Example.
<!DOCTYPE html>
<html>
<body>
<form method="post">
Name: <input type="text" name="name">
<input type="submit">
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = $_POST['name'];
echo "Hello, $name!";
}
?>
</body>
</html>
14. Design the HTML page which enters a given number and embeds the PHP
code to display a message indicating, whether the number is armstrong or not,
when clicking on the ‘CHECK NUMBER’ button.
<!DOCTYPE html>
<html>
<body>
<form method="post">
Enter a number: <input type="number" name="number">
<input type="submit" value="CHECK NUMBER">
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$number = $_POST['number'];
function isArmstrong($num) {
$sum = 0;
$temp = $num;
while ($temp != 0) {
$digit = $temp % 10;
$sum += $digit ** 3;
$temp = intval($temp / 10);
}
return $sum === $num;
}
echo isArmstrong($number) ? "$number is an Armstrong number" : "$number
is not an Armstrong number";
}
?>
</body>
</html>
JS
2. Design a form using html and JavaScript to collect the details from the Car
manufacturing company such as Car name, Car model (Radio Button), Colour
and
Rate. Create a dynamic drop down list for colour and based on the colour,
another
drop down list contains a rate.
<!DOCTYPE html>
<html>
<body>
<form>
Car Name: <input type="text" name="carName"><br>
Car Model: <input type="radio" name="carModel" value="Model1">Model1
<input type="radio" name="carModel" value="Model2">Model2<br>
Colour:
<select id="color" onchange="updateRate()">
<option value="Red">Red</option>
<option value="Blue">Blue</option>
</select><br>
Rate:
<select id="rate">
<option value="5000">5000</option>
</select>
</form>
<script>
function updateRate() {
let color = document.getElementById("color").value;
let rate = document.getElementById("rate");
rate.innerHTML = color === "Red" ? "<option value='5000'>5000</option>" :
"<option value='6000'>6000</option>";
}
</script>
</body>
</html>
4. Design a form using html and JavaScript to collect the details from the user
such
as name, gender (Radio Button), address, state, city. Create a dynamic drop
down list
for state and based on state another drop down list contains a list of cities for
state.
<!DOCTYPE html>
<html>
<body>
<form>
Name: <input type="text" name="name"><br>
Gender:
<input type="radio" name="gender" value="Male">Male
<input type="radio" name="gender" value="Female">Female<br>
Address: <input type="text" name="address"><br>
State:
<select id="state" onchange="updateCity()">
<option value="State1">State1</option>
<option value="State2">State2</option>
</select><br>
City:
<select id="city">
<option value="City1">City1</option>
</select>
</form>
<script>
function updateCity() {
let state = document.getElementById("state").value;
let city = document.getElementById("city");
city.innerHTML = state === "State1" ? "<option value='City1'>City1</option>"
: "<option value='City2'>City2</option>";
}
</script>
</body>
</html>
5. Design a login form using HTML & JavaScript with following validations on
username and password fields.
1. Password length must be 6 to 12 characters.
2. Username should not start with _, @ or number.
3. Phone number must be 10 digits starting with 6/7/8/9.
4. All fields should not be blank.
<!DOCTYPE html>
<html>
<body>
<form onsubmit="return validateForm()">
Username: <input type="text" id="username"><br>
Password: <input type="password" id="password"><br>
Phone Number: <input type="text" id="phone"><br>
<input type="submit" value="Login">
</form>
<script>
function validateForm() {
let username = document.getElementById("username").value;
let password = document.getElementById("password").value;
let phone = document.getElementById("phone").value;
if (!/^[^\d_@][\w@]{5,11}$/.test(username)) {
alert("Invalid username.");
return false;
}
if (password.length < 6 || password.length > 12) {
alert("Password must be 6 to 12 characters.");
return false;
}
if (!/^[6-9]\d{9}$/.test(phone)) {
alert("Phone number must be 10 digits starting with 6, 7, 8, or 9.");
return false;
}
return true;
}
</script>
</body>
</html>
document.addEventListener('click', function(event) {
console.log("Mouse Button clicked:", event.button);
console.log("Number of elements in form:",
document.forms[0].elements.length);
document.body.innerHTML = "Hello World";
});
document.addEventListener('keydown', function(event) {
console.log("Key code:", event.keyCode);
if ('aeiou'.includes(event.key.toLowerCase())) {
alert("Vowel is pressed");
}
});
document.addEventListener('keyup', function() {
document.body.style.backgroundColor = 'red';
});
9. List the order of precedence of style levels. Design a sample web page for
providing ‘KTU BTech Honours Regulation 19’ for KTU and use embedded Style
sheet to apply minimum 5 styles for list, tables and pages.
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: Arial; background-color: #f0f0f0; }
ul { list-style-type: square; color: red; }
table { border-collapse: collapse; width: 50%; }
td, th { border: 1px solid black; padding: 8px; }
h1 { color: blue; }
</style>
</head>
<body>
<h1>KTU BTech Honours Regulation 19</h1>
<ul>
<li>Requirement 1</li>
<li>Requirement 2</li>
</ul>
<table>
<tr>
<th>Subject</th>
<th>Credits</th>
</tr>
<tr>
<td>Mathematics</td>
<td>4</td>
</tr>
</table>
</body>
</html>