Professional Documents
Culture Documents
Solution PHP Practical QB 2022-23
Solution PHP Practical QB 2022-23
session_unset();
session_destroy();
echo"<br>sesion is session_destroy";
?>
</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";
}
?>
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();
?>
if($flag==1)
{
echo "<br>$n is prime number";
}
else
{
echo "<br>$n is not prime number";
}
?>
$email = "johnjodoh@gmail.com";
if (filter_var($email, FILTER_VALIDATE_EMAIL))
{
<?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";
}
?>
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'";
$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!";
}
?>
<?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 = "";
<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'];
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;
}
$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>";
}
?>
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>";
}
}
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);
}
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);
}
$conn->close();
?>
/ Subject Teacher