PHP Practical Codes Final
PHP Practical Codes Final
Code:
<?php
$n=18;
echo"Number=",$n,"<br>";
if($n%2==0)
echo"Number is even";
else
echo"Number is odd";
?>
Code:
<?php
$n=-11;
echo"Number=",$n,"<br>";
if($n>0)
echo"Number is positive";
elseif($n<0)
echo"Number is negative";
else
echo"Number is Zero"
?>
Code:
<?php
$month=5;
echo"Month=",$month,"<br>";
switch($month)
{
case 1:
echo"January";
break;
case 2:
echo"February";
break;
case 3:
echo"March";
break;
case 4:
echo"April";
break;
case 5:
echo"May";
break;
case 6:
echo"June";
break;
case 7:
echo"July";
break;
case 8:
echo"August";
break;
case 9:
echo"September";
break;
case 10:
echo"October";
break;
case 11:
echo"November";
break;
case 12:
echo"December";
break;
default:
echo"Invalid Month";
}
?>
Code:
<?php
$i=2;
echo "for loop:\n";
for($i=2; $i<=60; $i+=2)
echo $i," ";
$i=2;
echo "\n\nWhile loop:\n";
while($i<=60)
{
echo $i," ";
$i+=2;
}
$i=2;
echo "\n\nDo-While loop:\n";
do{
echo $i," ";
$i+=2;
}while($i<=60)
?>
Code:
<?php
for ($i = 5; $i >= 1; $i--) {
for ($j = 1; $j <= $i; $j++) {
echo "1";
}
echo "<br>";
}
?>
b) 54321
4321
321
21
1
Code:
<?php
for ($i = 5; $i >= 1; $i--) {
for ($j = $i; $j >= 1; $j--) {
echo $j;
}
echo "\n";
}
?>
c)
55555
4444
333
22
1
Code:
<?php
for($i = 5; $i >= 1; $i--){
for($j = 1; $j <= $i; $j++){
echo$i;
}
echo"<br>";
}
?>
d)
*
**
***
****
*****
Code:
<?php
for($i=1;$i<=5;$i++){
for($j=1;$j<=$i;$j++){
echo"*";
}
echo"<br>";
}
?>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
$no = $_POST["number"];
$no2 = strrev($no);
if($no == $no2)
echo "Palindrome";
else
echo "Not a Palindrome";
}
?>
</body>
</html>
interface Wheels {
function rotateWheels();
}
function rotateWheels() {
echo "Wheels rotating<br>";
}
}
class MyClass2 {
function __construct($num1, $num2) {
$sum = $num1 + $num2;
echo "<br>The sum of $num1 and $num2 is $sum<br>";
}
}
$ob = new MyClass();
$ob1 = new MyClass2(5, 5);
?>
15. Introspection and serialization
Code:
<?php
//Serialization
echo "Serialization:<br>";
$data = array(
'name' => 'John',
'age' => 30,
'city' => 'New York'
);
$serialized_data = serialize($data);
echo $serialized_data, "<br><br>";
$unserialized_data = unserialize($serialized_data);
print_r($unserialized_data);
//Introspection
echo "<br><br>Introspection:<br>";
class MyClass {
public $prop1 = "Property 1";
public $prop2 = "Property 2";
<!DOCTYPE html>
<html>
<head>
<title>Registration Form</title>
</head>
<body>
<form action="" method="post">
Full Name: <input type="text" name="name" required> <br><br>
Mobile Number: <input type="text" name="mbno"
maxlength="10" required><br><br>
Address: <textarea name="address" rows="3" cols="20"
required></textarea><br>
Gender:
<input type="radio" name="gender" value="male">Male
<input type="radio" name="gender" value="female">Female<br>
Select Subjects:<br>
<input type="checkbox" name="subjects[]"
value="Python">Python<br>
<input type="checkbox" name="subjects[]" value="Mobile
App">Mobile App<br>
<input type="checkbox" name="subjects[]"
value="PHP">PHP<br>
<input type="submit" name="submit">
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (!empty($_POST['gender'])) {
if (!empty($_POST['subjects'])) {
echo "Your Name: ", $_POST['name'], "<br>";
echo "Your Mobile Number: ", $_POST['mbno'], "<br>";
echo "Address:", $_POST['address'], "<br>";
echo "Gender:", $_POST['gender'], "<br>";
echo "Selected Subjects:";
foreach ($_POST['subjects'] as $checked) {
echo $checked, " ";
}
} else {
echo "Select subjects";
}
} else {
echo "Select gender";
}
}
?>
</body>
</html>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$color = $_POST['colors'] ?? null;
$hidden = $_POST['hidden'] ?? null;
$fruits = $_POST['fruits'] ?? null;
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$from = $_POST["from"];
$to = $_POST["to"];
$sub = $_POST["subject"];
$mess = $_POST["message"];
$header = "From: $from";
if (mail($to, $sub, $mess, $header)) {
echo "<H4><B>mail sent</B></H4>";
} else {
echo "<H4><B>fail to send email</B></H4>";
}
}
?>
</body>
</html>
$con=mysqli_connect($servername,$username,$password,$db);
if(!$con)
echo "Something went wrong";
else
echo"Database connected";
$sql="INSERT INTO employee (empid,name,age) VALUES
(01,'JOHN',18)";
if(mysqli_query($con,$sql))
echo"Record inserted";
else
echo"Error";
?>
if (!$con) {
echo "Something went wrong";
} else {
echo "Database connected";
}
if (mysqli_query($con, $sql)) {
echo "Record Updated";
} else {
echo "Error";
}
?>
25. Delete data from database
Code:
<?php
$hostname = "localhost";
$db = "test";
$username = "root";
$password = "";
$con = mysqli_connect($hostname, $username, $password, $db);
if (!$con) {
echo "Something went wrong";
} else {
echo "Database connected";
}
if (mysqli_query($con, $sql)) {
echo "Record Deleted";
} else {
echo "Error";
}
?>
26. Write a script to create an image.
Note: Enable gd extension from php.ini (config file)
Steps: (For Understanding)
1. Open Xamp -> Click on Config of Apache
2. Click on PHP(php.ini)
3. Search the following line ;extension=gd
4. Remove the semicolon from the beginning of the line to
uncomment it and save the changes
5. Restart Xampp
Code:
<?php
// Set content type to PNG image
header("Content-Type: image/png");
// Free memory
imagedestroy($img);
?>
27. Write a program to create a PDF file.
Note: Download fpdf zip file from google
Code:
<?php