0% found this document useful (0 votes)
65 views

Solution PHP Practical QB 2022-23

The document provides PHP code examples for various tasks like session management, form validation, checking even/odd numbers, creating PDFs, checking prime numbers, validating emails, implementing string functions, math functions, replacing strings, sending emails, checking positive/negative numbers, inserting into databases, creating login pages, printing arrays, and using switch statements. A variety of programming concepts are demonstrated including loops, functions, arrays, SQL, and OOP.

Uploaded by

Sneha Nikam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
65 views

Solution PHP Practical QB 2022-23

The document provides PHP code examples for various tasks like session management, form validation, checking even/odd numbers, creating PDFs, checking prime numbers, validating emails, implementing string functions, math functions, replacing strings, sending emails, checking positive/negative numbers, inserting into databases, creating login pages, printing arrays, and using switch statements. A variety of programming concepts are demonstrated including loops, functions, arrays, SQL, and OOP.

Uploaded by

Sneha Nikam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

K. K. Wagh Polytechnic, Nashik.

Hirabai Haridas Vidyanagari, Amrutdham, Panchavati, Nashik-422003


Phone : 0253 -2517006, 2517005 Website : https://poly.kkwagh.edu.in
Email : disp-poly@kkwagh.edu.in principal-poly@kkwagh.edu.in
DEPARTMENT OF COMPUTER TECHNOLOGY
Academic Year: 2022-23 Class: TYCM-I, II & III Course/Year: CM6I
Course: Web Based Development Using PHP (WBP)

Course Code: 22619

1. Write a program to create a session, update, and destroy a session.


<?php
session_start();//start session
$_SESSION['name']="wiliam";
$_SESSION['place']="london";
$_SESSION['planet']="Earth";

echo "session is set is set<br>";


echo "<br>Name= ".$_SESSION['name'];
echo "<br>place= ".$_SESSION['place'];
echo "<br>planet= ".$_SESSION['planet'];

echo "<br><br>session update";


$_SESSION['name']="john";
$_SESSION['place']="perth";
$_SESSION['planet']="mars";

echo "<br>Name= ".$_SESSION['name'];


echo "<br>place= ".$_SESSION['place'];
echo "<br>planet= ".$_SESSION['planet'];

session_unset();
session_destroy();

echo"<br>sesion is session_destroy";
?>

2. Write a program to use validation for following controls


a) Username
b) Email address
Input .php-
<html>
<body>
<form method="POST" action="validate.php">
Enter User Name <input type="text" name="username"> <br>
Enter Email Address <input type="email" name="email_addr">
<br>
<input type="submit" name="submit">
</form>

</body>
</html>

Validate .php –
<?php
$name=$_POST['username'];
$email=$_POST['email_addr'];

if (!filter_var($email,FILTER_VALIDATE_EMAIL))
{
echo "Enter correct email id<br>";
}
else{
echo "$email <br>email is correct<br>";
}
if (!preg_match ("/^[a-zA-z]*$/", $name) )
{
echo "Only alphabets and whitespace are allowed.";
}
else {
echo "$name <br> name is correct";
}
?>

3. WAP to simply find even and odd no from 1 to 10.


<?php
$a=1;
for ($i=1; $i <=10 ; $i++) {
if ($i%2==0){
echo "Even : $i<br>";
}
else
{
echo "odd : $i<br>" ;
}
}
?>

4. Write a simple PHP program to create a PDF documents by using Graphics concepts.

<?php
require('fpdf185/fpdf.php');
$pdf=new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',18);
$pdf->Cell(40,10,'this is pdf doucumetn');
$pdf->output();
?>

5. Write a PHP program to check prime number


<?php
$n=43;
$i=2;
$flag=1;
while ($i<=$n)
{
if($n%$i==0)
{
$flag=0;
break;
}
$i++;
}

if($flag==1)
{
echo "<br>$n is prime number";
}
else
{
echo "<br>$n is not prime number";
}
?>

6. Write a simple PHP program to check that email id is valid or not


<?php

$email = "johnjodoh@gmail.com";
if (filter_var($email, FILTER_VALIDATE_EMAIL))
{

echo "$email <br> is valid Email ID";


}
else
{
echo "$email<br> is not valid Email ID";
}
?>

7. Write a PHP script to Implement String Function

<?php
$s="hello world!";
echo strlen("Hello world!");
echo "<br>";
echo str_word_count("Hello world!");
echo "<br>";
echo strrev("Hello world!");
echo "<br>";
echo strpos("Hello world!", "world");
echo "<br>";
echo str_replace("world", "Dolly", "Hello world!");
echo "<br>";
echo ucwords($s);
echo "<br>";
echo strtoupper($s);
echo "<br>";
echo strtolower($s);
echo "<br>";
echo str_repeat($s,3);
echo "<br>";
echo strcmp($s,"dafklfdfd adfklj");
echo "<br>";
?>
8. Write a PHP script to Implement Math Function
<?php
echo(pi());
echo(min(0, 150, 30, 20, -8, -200));
echo(max(0, 150, 30, 20, -8, -200));
echo(abs(-6.7));
echo(sqrt(64));
echo(round(0.60));
echo(round(0.49));
echo(rand());
echo(rand(10, 100));
?>

9. Write a PHP script to replace the first ‘the’ of the following string with ‘best’

<?php
$str = 'Techstudy - the Debugging Solution website';
echo $str;
$a=str_replace("the", "best",$str);
echo "<br>$a";
?>

10. Write a PHP program for sending and receiving plain text message (email).

<?php
$to_email="siddeshamrutkar40@gmail.com";
$subject="simple Email Test";
$body = "Hi gayatri , This is mail for testing the code";
$headers="From: siddeshamrutkar40@gmail.com ";
if(mail($to_email, $subject, $body, $headers))
{
echo "Email successfully sent to $to_email";
}
else
{
echo "Email Not Send";
}
?>

11. Write a program to check no is positive or negative.


<?php
$a=1;
if ($a>0){
echo "number is positive";
}
else {
echo "number is negative";
}
?>

12. Write a PHP code to insert data into employee table.


<?php
$conn=mysqli_connect("localhost","root","","cont");
$sql1="INSERT INTO employee VALUES('1','John','smith',20)";
if ($conn->connect_error){
die("connection failed".$conn->connect_error);
}
if ($conn->query($sql1)===TRUE){
echo "insert succesfully";
}
else
{
echo "not insert";
}
?>

13. PHP Program to create a login page


Input.php –
<html>
<body>
<h1>Login</h1>
<form action="login.php" method="POST">
Username:<input type="text" name="usr"><br><br>
Password:<input type="password" name="pass"><br><br><br>
<input type="submit" value="login">
</form>
</body>
</html>

Login.php –
<?php
if ($_POST) {
$usrname = $_POST['usr'];
$passwd = $_POST['pass'];
$dbname = "cont";
$con = mysqli_connect("localhost","root", "",$dbname);
if ($con) {
echo "Successfully Connected.<br>";
$qry ="SELECT username,password FROM login WHERE username='$usrname' AND
password='$passwd'";

$result = mysqli_query($con, $qry);


$nos = mysqli_num_rows($result);
if ($nos)
echo " <br> $usrname, You are Logged Successfully...";
else
echo "Incorrect username or password";
}
else{
echo "Error Connecting to the database";
}
}
?>

14. Program to print each element of an array using foreach().


<?php

$arr = array(10,20,30,40,50);

foreach($arr as $v)
{
echo $v;
echo "<br>";
}

?>
15. Write a program to show day of the week using switch
<?php
$day = "5";

switch ($day) {
case "1":
echo "It is Monday!";
break;
case "2":
echo "It is today!";
break;
case "3":
echo "It is Wednesday!";
break;
case "4":
echo "It is Thursday!";
break;
case "5":
echo "It is Friday!";
break;
case "6":
echo "It is Saturday!";
break;
case "7":
echo "It is Sunday!";
break;
default:
echo "Invalid number!";
}
?>

16. Write a factorial program using for loop in php


<?php
$n =5;
$f = 1;
for ($i = 1; $i <= $n; $i++)
{
$f *= $i;
}
echo "The factorial of $n is $f";
?>

17. Write a simple calculator program in PHP using switch case


<!DOCTYPE html>
<head>
<title>Simple Calculator Program in PHP - Tutorials Class</title>
</head>

<?php
$first_num = $_POST['first_num'];
$second_num = $_POST['second_num'];
$operator = $_POST['operator'];
$result = '';
if (is_numeric($first_num) && is_numeric($second_num)) {
switch ($operator) {
case "Add":
$result = $first_num + $second_num;
break;
case "Subtract":
$result = $first_num - $second_num;
break;
case "Multiply":
$result = $first_num * $second_num;
break;
case "Divide":
$result = $first_num / $second_num;
}
}

?>

<body>
<div id="page-wrap">
<h1>PHP - Simple Calculator Program</h1>
<form action="" method="post" id="quiz-form">
<p>
<input type="number" name="first_num" id="first_num" required="required"
value="<?php echo $first_num; ?>" /> <b>First Number</b>
</p>
<p>
<input type="number" name="second_num" id="second_num" required="required"
value="<?php echo $second_num; ?>" /> <b>Second Number</b>
</p>
<p>
<input readonly="readonly" name="result" value="<?php echo $result; ?>">
<b>Result</b>
</p>
<input type="submit" name="operator" value="Add" />
<input type="submit" name="operator" value="Subtract" />
<input type="submit" name="operator" value="Multiply" />
<input type="submit" name="operator" value="Divide" />
</form>
</div>
</body>
</html>
18. Write a PHP program to reverse the string without string function
<?php
$string = "Hello, World!";
$length = strlen($string);
$reversed = "";

for ($i = $length - 1; $i >= 0; $i--)


{
$reversed .= $string[$i];
}
echo "Original string: " . $string . "<br>";
echo "Reversed string: " . $reversed;
?>

19. Write a PHP program to count the words in the string


<?php
$sample_words = "Write a PHP program to count the words in the string";
echo str_word_count($sample_words);
?>

20. Write a program to check entered year is Leap Year or not.

<html>
<body>
<form action="" method="post" id ="fact">
<p>
<b>Enter the Year</b><input type="text" name="year" id="year"
required="required"/>
</p>
<input type="submit" name="operator" value="Check " />
</form>
</body>
<?php
$year = $_POST['year'];

if (($year % 4 == 0 && $year % 100 != 0) || $year % 400 == 0)


{
echo $year . " is a leap year";
}
else
{
echo $year . " is not a leap year";
}
?>
</html>

21. Write calendar program using switch


<?php
$day = "5";
switch ($day) {
case "1":
echo "It is Monday!";
break;
case "2":
echo "It is today!";
break;
case "3":
echo "It is Wednesday!";
break;
case "4":
echo "It is Thursday!";
break;
case "5":
echo "It is Friday!";
break;
case "6":
echo "It is Saturday!";
break;
case "7":
echo "It is Sunday!";
break;
default:
echo "Invalid number!";
}
?>

Or

<?php
$month = 5;
$year = 2023;

switch ($month)
{
case 2:
if (($year % 4 == 0 && $year % 100 != 0) || $year % 400 == 0)
{
$days_in_month = 29;
} else
{
$days_in_month = 28;
}
break;
case 4:
case 6:
case 9:
case 11:
$days_in_month = 30;
break;
default:
$days_in_month = 31;
break;
}

$weekday = date("N", strtotime($year . "-" . $month . "-01"));


echo " " . date("F Y", strtotime($year . "-" . $month . "-01")) . "\n";
echo "<br> M T W T F S S <br>";

$day = 1;
for ($row = 1; $row <= 6; $row++)
{
for ($col = 1; $col <= 7; $col++)
{
if ($row == 1 && $col < $weekday)
{
echo " ";
} elseif ($day > $days_in_month)
{
break;
} else
{
printf("%3d", $day++);
}
echo " ";
}
if ($day > $days_in_month)
{
break;
}
echo "<br>";
}
?>

22. WAP to print first 30 even number


<?php
$count = 0;
$num = 0;

while ($count < 30)


{
if ($num % 2 == 0)
{
echo $num . " ";
$count++;
}
$num++;
}
?>

23. Write a program to find reverse of a number


<?php
$num = 12345;
echo "original number is: " .$num;
$reverse = 0;
while ($num != 0)
{
$digit = $num % 10;
$reverse = $reverse * 10 + $digit;
$num = intval($num / 10);
}

echo "<br>Reverse of the number is: " .$reverse;


?>

24. WAP to implement associative array


<?php
$fruits = array(
"apple" => "red",
"banana" => "yellow",
"orange" => "orange",
"grape" => "purple"
);
foreach ($fruits as $fruit => $color)
{
echo "The color of a " .$fruit . " is " . $color . "<br>";
}
?>

25. WAP to implement index array


<?php

$numbers = array(10, 20, 30, 40, 50);

echo "The value at index 2 is " . $numbers[2] . "<br>";


foreach ($numbers as $number) {
echo $number . " ";
}
?>

26. WAP to count no of words in string without string function


<?php
$string = "Write a PHP program to count the words in the string";
$length = strlen($string);
$count = 0;

for ($i = 0; $i < $length; $i++)


{
if ($string[$i] === " " || $i === $length - 1)
{
$count++;
}
}
echo "Word count: " . $count;
?>

27. WAP to implement Single inheritance


<?php
class Animal
{
public $name;

function __construct($name)
{
$this->name = $name;
}
function eat()
{
echo $this->name . " is eating.<br>";
}
}
class Dog extends Animal
{
function bark()
{
echo $this->name . " is barking.<br>";
}
}

$dog = new Dog("Fido");


$dog->eat();
$dog->bark();
?>

28. Write a program to delete 1 record from Student Database where RollNo=2
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "mydb";
$conn = new mysqli($servername, $username, $password, $dbname);

if ($conn->connect_error)
{
die("Connection failed: " . $conn->connect_error);
}

$sql = "DELETE FROM stud WHERE RollNo=2";

if ($conn->query($sql) === TRUE)


{
echo "Record deleted successfully";
}
else
{
echo "Error deleting record: " . $conn->error;
}
$conn->close();
?>

30. Write a program to update 1 record from Student Database. Update name of
Student having RollNo=1
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "mydb";
$conn = new mysqli($servername, $username, $password, $dbname);

if ($conn->connect_error)
{
die("Connection failed: " . $conn->connect_error);
}

$sql = "UPDATE stud SET Name='12' WHERE RollNo=1";

if ($conn->query($sql) === TRUE)


{
echo "Record updated successfully";
}
else
{
echo "Error updating record: " . $conn->error;
}

$conn->close();
?>

Mr. P. S. Chavan / Mrs. S.R. Jagtap

/ Subject Teacher

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy