PHP 29 1
PHP 29 1
- 29
Q1 Create a simple HTML form and accept the user name and display the name
through PHP echo statement.
Code =>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<label for="name">Name:</label>
<button type="submit">Submit</button>
</form>
?>
</body>
</html>
Code :
<?php
function isPrime($number) {
if ($number <= 1) {
return false;
if ($number <= 3) {
return true;
if ($number % 2 == 0 || $number % 3 == 0) {
return false;
return false;
return true;
$number = 64;
if (isPrime($number)) {
} else {
?>
Code =
<?php
$number = 48;
if ($number % 2 == 0) {
} else {
?>
Code =
<?php
$num1 = 41;
$num2 = 18;
$num3 = 48;
} else {
?>
Code =
<?php
?>
Q6 Create a script that displays 1-2-3-4-5-6-7-8-9-10 on one line. There will be no hyphen(-
)at starƟng and ending posiƟon
Code =
<?php
echo $i;
if ($i != 10) {
echo "-";
?>
Q7 Write a PHP program to generate and display the first n lines of a Floyd triangle.
Sample output for n = 5 :12 34 5 67 8 9 1011 12 13 14 15
Code =
<?php
$n = 5;
$num = 1;
$num++;
}
echo "<br>";
?>
Q8 .Write a PHP program to generate and display the first n lines of a Floyd triangle.
Sample output for n = 5 :12 34 5 67 8 9 1011 12 13 14 15
Code =
<?php
$userNumber = 7;
} else {
if ($i == $userNumber) {
continue;
}
echo $i . " ";
?>
Q9. Write a PHP program to generate and display the first n lines of a Floyd triangle.
Sample output for n = 5 :12 34 5 67 8 9 1011 12 13 14 15
Code =
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$value1 = $_POST["value1"];
$value2 = $_POST["value2"];
$action = $_POST["action"];
$result = 0;
switch ($action) {
case "add":
break;
case "subtract":
break;
case "multiply":
break;
case "divide":
if ($value2 != 0) {
} else {
break;
?>
<!DOCTYPE html>
<html>
<head>
<title>ArithmeƟc Calculator</title>
</head>
<body>
<h2>Arithmetic Calculator</h2>
<option value="add">Add</option>
<option value="subtract">Subtract</option>
<option value="multiply">Multiply</option>
<option value="divide">Divide</option>
</select><br><br>
<button type="submit">Calculate</button>
</form>
<h3>Result:</h3>
</body>
</html>
Q10. Write a program to print the mulƟplicaƟon table of the number entered by the
user
Code =
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$number = $_POST["number"];
?>
<!DOCTYPE html>
<html>
<head>
<Ɵtle>MulƟplicaƟon Table</Ɵtle>
</head>
<body>
<h2>MulƟplication Table</h2>
</form>
<table border="1">
<tr>
<th>Multiplier</th>
<th>Result</th>
</tr>
<?php
echo "<tr>";
echo "<td>$i</td>";
echo "<td>$result</td>";
echo "</tr>";
?>
</table>
</body>
</html>
Q11. Write a program to check whether the given number is Armstrong or not.
Code =
<?php
function isArmstrong($num) {
$originalNum = $num;
$sum = 0;
$numDigits = strlen($num);
$number = 153;
if (isArmstrong($number)) {
} else
?>
Q12. Write a Program to display fruit names & that values within a table
Code =
<!DOCTYPE html>
<html>
<head>
<title>Fruit Table</title>
</head>
<body>
<h2>Fruit Table</h2>
<table border="1">
<tr>
<th>Fruit Name</th>
<th>Value</th>
</tr>
<?php
$fruits = array(
"Apple" => 1,
"Banana" => 2,
"Orange" => 3,
"Watermelon" => 5,
"Mango" => 6,
"Pineapple" => 7,
"Strawberry" => 8,
"Peach" => 10
);
echo "<tr>";
echo "<td>$fruit</td>";
echo "<td>$value</td>";
echo "</tr>";
?>
</table>
</body>
</html>
Code =
<?php
$numbers = array(64,74);
$sum = 0;
$sum += $number;
?>
Q14 .Write a program to find the greatest and the smallest element of the given array.
Code =
<?php
$min = $numbers[0];
$max = $numbers[0];
$min = $number;
$max = $number;
?>
Q15. Write a program to find the greatest and the smallest element of the given array.
Code =
<?php
$matrix = array(
array(1, 2, 3),
array(4, 5, 6),
array(7, 8, 9)
);
$userNumber = 3;
}
echo "<br>";
echo "<br>";
?>
Code =
<?php
function isPalindrome($string) {
$string = strtolower($string);
$reversedString = strrev($string);
$testString = "drashti";
if (isPalindrome($testString)) {
} else {
?>
Q17. WritetheprogramtogeƩheregistraƟonpageinformaƟonin
htmlandshowthaƟnformaƟonusingPHP
Code =
17-1.php =
<!DOCTYPE html>
<html>
<head>
<title>Registration Page</title>
</head>
<body>
<h2>Registration Page</h2>
<label for="name">Name:</label>
<label for="email">Email:</label>
<label for="username">Username:</label>
<label for="password">Password:</label>
<button type="submit">Register</button>
</form>
</body>
</html>
17-2.php =
<!DOCTYPE html>
<html>
<head>
<title>User Information</title>
</head>
<body>
<h2>User Information</h2>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = $_POST["name"];
$email = $_POST["email"];
$username = $_POST["username"];
$password = $_POST["password"];
}
?>
</body>
</html>
Code =
File 1
<!DOCTYPE html>
<html>
<head>
<title>Login Page</title>
</head>
<body>
<h2>Login Page</h2>
<label for="username">Username:</label>
<label for="password">Password:</label>
<button type="submit">Login</button>
</form>
</body>
</html>
File 2
<!DOCTYPE html>
<html>
<head>
<title>Login Status</title>
</head>
<body>
<h2>Login Status</h2>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$username = $_POST["username"];
$password = $_POST["password"];
$correctUsername = "admin";
} else {
?>
</body>
</html>
Q19 .Create the pages for fruits and vegetables, write the program to display the selected
item in separate page using cookies
Code =
19-1.html
<!DOCTYPE html>
<html>
<head>
<title>Fruits Page</title>
</head>
<body>
<h2>Fruits Page</h2>
<option value="Apple">Apple</option>
<option value="Banana">Banana</option>
<option value="Orange">Orange</option>
</select><br><br>
<button type="submit">Submit</button>
</form>
</body>
</html>
19-2.php
<!DOCTYPE html>
<html>
<head>
<title>Vegetables Page</title>
</head>
<body>
<h2>Vegetables Page</h2>
<option value="Carrot">Carrot</option>
<option value="Broccoli">Broccoli</option>
<option value="Spinach">Spinach</option>
</select><br><br>
<button type="submit">Submit</button>
</form>
</body>
</html>
19-3.php
<!DOCTYPE html>
<html>
<head>
<title>Selected Item</title>
</head>
<body>
<h2>Selected Item</h2>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$selectedItem = $_POST["item"];
}
?>
</body>
</html>
Q20. Create the pages for fruits and vegetables with their rates, no. of qty, write the
program to calculate the subtotal & total display the selected item and subtotal & total
display in separate page using cookies .
Code =
File 1
<!DOCTYPE html>
<html>
<head>
<title>Fruits Page</title>
</head>
<body>
<h2>Fruits Page</h2>
<option value="Apple">Apple</option>
<option value="Banana">Banana</option>
<option value="Orange">Orange</option>
</select><br><br>
<label for="quantity">Quantity:</label>
<label for="rate">Rate:</label>
<button type="submit">Calculate</button>
</form>
</body>
</html>
20-2.php
<!DOCTYPE html>
<html>
<head>
<title>Vegetables Page</title>
</head>
<body>
<h2>Vegetables Page</h2>
<option value="Carrot">Carrot</option>
<option value="Broccoli">Broccoli</option>
<option value="Spinach">Spinach</option>
</select><br><br>
<label for="quantity">Quantity:</label>
<label for="rate">Rate:</label>
<button type="submit">Calculate</button>
</form>
</body>
</html>
20-3.php
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$selectedItem = $_POST["item"];
$quantity = $_POST["quantity"];
$rate = $_POST["rate"];
if (isset($_COOKIE["total"])) {
} else {
$total = $subtotal;
?>
</body>
</html>
Code =
File 1
<!DOCTYPE html>
<html>
<head>
<title>Login Page</title>
</head>
<body>
<h2>Login Page</h2>
<label for="username">Username:</label>
<label for="password">Password:</label>
<button type="submit">Login</button>
</form>
</body>
</html>
21-2.php
<?php
session_start();
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$username = $_POST["username"];
$password = $_POST["password"];
$correctUsername = "admin";
$correctPassword = "password";
$_SESSION["username"] = $username;
header("Location: 21-3.php");
exit();
} else {
header("Location: 21-4.php");
exit();
?>
21-3.php
<?php
session_start();
if (!isset($_SESSION["username"])) {
header("Location: 21-3.php");
exit();
?>
<!DOCTYPE html>
<html>
<head>
<title>Welcome</title>
</head>
<body>
<p><a href="21-4.php">Logout</a></p>
</body>
</html>
21-4.php
<?php
session_start();
session_unset();
session_destroy();
header("Location: 21-1.html");
exit();
?>
Q22. Create the page to get the employee personal informaƟon and store the informaƟon
in mysqldb.
Code =
File 1
<!DOCTYPE html>
<html>
<head>
<title>Employee Information</title>
</head>
<body>
<h2>Employee Information</h2>
<label for="email">Email:</label>
<label for="phone">Phone:</label>
<button type="submit">Submit</button>
</form>
</body>
</html>
22-2.php
<?php
$hostname = "localhost:3307";
$username = "root";
$password = "";
$dbname = "test22";
if ($conn->connect_error) {
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$first_name = $_POST["first_name"];
$last_name = $_POST["last_name"];
$email = $_POST["email"];
$phone = $_POST["phone"];
} else {
$conn->close();
?>
Q23 Write a PHP script which will display the copyright information in the following
format. To get current year you can use the date() function. Expected Format : © 2013
PHP Exercises24.
Code =
<?php
$currentYear = date("Y");
?>
Q24. Write a PHP script to print the current date in the following format. To get current
date's information you can use the date() function. Sample format : (assume current date
is September 01, 2013)2013/09/0113.09.0101-09-13
Code=
<?php
$currentDate = date("Y/m/d");
$shortYear = date("y");
$monthWithLeadingZero = date("m");
$dayWithLeadingZero = date("d");
echo "$currentDate$shortYear.$monthWithLeadingZero.$dayWithLeadingZero-
$monthWithLeadingZero-$shortYear";
?>
Q25 .create your own web page using session, database & date concepts
Code =
File 1
<!DOCTYPE html>
<html>
<head>
<title>Login</title>
</head>
<body>
<h2>Login</h2>
<label for="username">Username:</label>
<label for="password">Password:</label>
<button type="submit">Login</button>
</form>
</body>
</html>
25-2.php
<?php
session_start();
$hostname = "localhost:3307";
$db_username = "root";
$db_password = "";
$dbname = "mystu";
if ($conn->connect_error) {
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$username = $_POST["username"];
$password = $_POST["password"];
$sql = "SELECT id FROM user WHERE username = '$username' AND password = '$password'";
$result = $conn->query($sql);
if ($result->num_rows === 1) {
$_SESSION["username"] = $username;
header("LocaƟon: 25-2.php");
exit();
} else {
header("Location: 25-3.php");
exit();
$conn->close();
?>
25-3.php
File3
<?php
session_start();
if (!isset($_SESSION["username"])) {
header("Location: login.html");
exit();
?>
<!DOCTYPE html>
<html>
<head>
<title>Welcome</title>
</head>
<body>
<p><a href="25-4.php">Logout</a></p>
</body>
</html>
25-4.php
<?php
session_start();
session_unset();
session_destroy();
header("Location: 25-1.html");
exit();
?>