0% found this document useful (0 votes)
99 views32 pages

PHP Practical

This document contains the code and output for 11 PHP practical assignments completed by a student. It covers basic PHP concepts like variables, operators, control structures, functions and classes. The practicals include printing text, calculations using operators, if/else/switch statements, while/for/foreach loops, string functions, simple and parameterized functions, OOP concepts like classes and inheritance, creating PDFs, serialization, traits and interfaces. Form validation using PHP is also demonstrated. Each practical has the PHP code and expected output.

Uploaded by

Mohit Bhangale
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)
99 views32 pages

PHP Practical

This document contains the code and output for 11 PHP practical assignments completed by a student. It covers basic PHP concepts like variables, operators, control structures, functions and classes. The practicals include printing text, calculations using operators, if/else/switch statements, while/for/foreach loops, string functions, simple and parameterized functions, OOP concepts like classes and inheritance, creating PDFs, serialization, traits and interfaces. Form validation using PHP is also demonstrated. Each practical has the PHP code and expected output.

Uploaded by

Mohit Bhangale
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/ 32

Name :- Pokharna Jay Sushil

Roll no:- 47
Enrollment no:- 1801300091
Class:- TYCM
Subject :- PHP
Practical no1:-
a) Write a program to print “Welcome to php”
CODE:-
<?php
echo"welcome to PHP";
?>
OUTPUT:-
b) Write a simple PHP program using expressions and
operators.
CODE:-
Operators:-

<?php
$i = 10;
$j = 12;
$sum = $i + $j;
echo"addition is {$sum}<br>";
$sub = $j - $i;
echo"Substraction is {$sub}<br>";
$mul = $i * $j;
echo"multiplication is {$mul}<br>";
$div = $i / $j;
echo"division is {$div}<br>";
?>
Output:-

Expression:-
Code:-
<?php
$i = 40;
$j = 80;
echo"</br>And operation: ".($i&$j);
echo"</br> Expression:-";
$op = ($i+$j)*20/10;
echo"</br> ($i+$j)*20/10 = ".$op;
?>

Output:-
Practical No 2:- Write a php program to
demonstrate the use of decision making
control structures using:-
a) If statement
CODE:-

<?php

$i=2;
if($i%2==0)
{
echo"<h1>If statement</h1><br>";
echo"<h2>I am Even<br></h2>";
}
?>
OUTPUT:-
b) If-else Statement
CODE:-
<?php
$i=23;
echo"If-else statement<br>";
if($i%2==0)
{
echo"I am Even<br>";
}
else{
echo"I am odd<br>";
}
?>
OUTPUT:-
c) Switch Statement:-
CODE:-
<?php
$i=2;
echo"switch statement<br>";
switch($i){
case "1": echo"sunday";
break;
case "2": echo"monday";
break;
case "3": echo"tuesday";
break;
case "4": echo"wednesday";
break;
case "5": echo"thursday";
break;
case "6": echo"friday";
break;
case "7": echo"saturday";
break;
default:echo"wrong input"; }
?>
OUTPUT:-
Practical no 3:- Write a php program to
demonstrate the use of looping
structures using :-
a)While statement :-
CODE:-
<?php
echo"Looping statements<br>";
$num=1;
echo"while loop<br>";
while($num < 4)
{
$num += 1;
echo "$num<br>";
}
?>
OUTPUT:-
b) Do-while statements :-
CODE:-
<?php
echo"Looping statements<br>";
echo"do-while loop<br>";
$num=0;
do{
echo "$num<br>";
$num++;
}while($num < 7);
?>

OUTPUT:-
c) FOR statement :-
CODE:-
<?php
echo"Looping statements<br>";
echo"For loop<br>";
for($i=6;$i<9;$i++)
{
echo"$i<br>";
}
?>

OUTPUT:-
d) FOR-EACH statement :-
CODE:-
<?php
echo"Looping statements<br>";
echo"For-each loop<br>";
$heroes = array("batman","robin","batgirl");
foreach($heroes as $hero)
{
echo $hero .",";
}
?>

OUTPUT:-
Practical no5:-
a) Write a php program to:-
i) Calculate the length of string.
CODE:-
<?php
$string="I am Jay";
echo"</br>Given string is : ".$string;
echo"</br>length of string :".strlen($string);
?>

OUTPUT:-
ii) Count the number of words in string without using
string function
CODE:-
<?php
//function that counts the number of words in a string
function get_str_num_words($string){
$string = preg_replace('/\s+/', ' ', trim($string));
$words = explode(" ", $string);
return count($words);
}
//String variable
$mystr = "I am Jay";
//Call function to get number of words
$mylen = get_str_num_words($mystr);
//Print number of words in the given string
echo"</br>given string:".$mystr;
echo "</br>Number of words in given string:".$mylen;
?>
OUTPUT:-
b) Write a simple php program to demonstrate use of
various built-in string functions.
CODE:-
<?php
$str="India is my country";
echo"<br>Given String:".$str;
echo"<br>String Functions:-";
echo"<br>string length:" .strlen($str);
echo"<br>Words in String:" .str_word_count($str);
echo"<br>Reverse string:" .strrev($str);
echo"<br>String in uppercase:" .strtoupper($str);
echo"<br>String in lowercase:" .strtolower($str);
echo"<br>position of india in string:" .strpos($str,"India");
echo"<br>Replace string:" .str_replace("my","our",$str);
?>
OUTPUT:-
Practical no.6 Write a simple php program to
demonstrate the use of simple function and
parameterized function.

Simple function:-
Code:-
<?php
function red()
{
echo"Roses are red!!!";
}
$fun_var="red";
$fun_var();
?>
Output:-
Parameterized function:-
Code:-
<?php
$addition=function ($x,$y)
{
echo"Addition is:".($x+$y);
};
echo$addition(10,20)
?>

Output:-
Practical no.7 Write a simple php program to
create pdf document by using graphic concepts.
Code:-
<?php
require("fpdf.php");
$pdf=new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',20);
$pdf->Cell(100,100,"This is my first pdf page using php!");
$pdf->Output();
?>
Output:-
Practical no.8 Write a php program to:
1.Write a simple php program on Introspection.
Code:-
<?php
class Boss
{
public function display()
{
echo"I am parent class";
}
}
class child extends Boss
{
public function display()
{
echo"I am".get_class($this),"class <br>";
echo"I am".get_parent_class($this),"'s child <br>";
}
}
if(class_exists("Boss"))
{
$s=new Boss();
echo"The class name is:". get_class($s)."<br>";
$s->display();
if(class_exists("child"))
{
$c=new child();
echo"The class name is: ".get_class($c)."<br>";
$c->display();
}
else{
echo"No,".get_class($c)."is not subclass of parent<br>";
}
}
?>
Output:-

2. Write a php program on serialization.


Code:-
<?php
$s=array(23,31,10,7,3,13,9,18);
$g=serialize($s);
print_r($g);
$p=unserialize($g);
echo"<br>";
print_r($p);
?>

Output:-
Practical no 9: Write a php program to
implement Multiple inheritance using
Traits.
Code:-
<?php
class Person
{
public Function display()
{
echo"Parent class -- Person <br>";
}
}
trait male
{
public function display1()
{
echo"parent class- male<br>";
}
}
class female extends Person
{
use male;
public function display2()
{
echo"Child class - female<br>";
}
}
$obj = new female();
$obj->display();
$obj->display1();
$obj->display2();
?>

Output:-
Practical no 10: Write a php program to
implement Multiple inheritance using
Interface.
Code:
<?php
class Father
{
public function fn1()
{
printf("Fun1() is called<br>");
}
}
interface inf
{
public function fn2();
}
class child extends Father implements inf
{
function fn2()
{
printf("fun2 is called <br>");
}
function fn3()
{
print("fun3 is called <br>");
}
}
$c1= new child();
$c1->fn1();
$c1->fn2();
$c1->fn3();
?>

Output:
Practical no 11: Design a web page using
following form controls: a. text box , b.
radio buttons c. check box d. buttons
CODE:-
<html>
<body>
<form action=”welcome.php” method=”get”>
<input type=”text” name=”user” />
<br>
<input type="radio" id="male" name="gender" value="male">
<label for="male">Male</label><br>
<input type="radio" id="female" name="gender" value="female
">
<label for="female">Female</label><br>
<input type="radio" id="other" name="gender" value="other">
<label for="other">Other</label>
<br>
<input type="checkbox" id="vehicle1" name="vehicle1" value=
"Bike">
<label for="vehicle1"> I have a bike</label><br>
<input type="checkbox" id="vehicle2" name="vehicle2" value=
"Car">
<label for="vehicle2"> I have a car</label><br>
<input type="checkbox" id="vehicle3" name="vehicle3" value=
"Boat">
<label for="vehicle3"> I have a boat</label><br>
<br>
<input type="button" class="w3-button w3-
black" value="Input Button">
<button class="w3-button w3-black">Submit</button>
</form>
</body>
</html>

OUTPUT:-
Practical no: 12 Develop a webpage with
data validation.
CODE:-
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<?php
// define variables to empty values
$nameErr = $emailErr = $mobilenoErr = $genderErr = $websiteErr = $a
greeErr = "";
$name = $email = $mobileno = $gender = $website = $agree = "";

//Input fields validation


if ($_SERVER["REQUEST_METHOD"] == "POST") {

//String Validation
if (empty($_POST["name"])) {
$nameErr = "Name is required";
} else {
$name = input_data($_POST["name"]);
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
$nameErr = "Only alphabets and white space are allowed";
}
}

//Email Validation
if (empty($_POST["email"])) {
$emailErr = "Email is required";
} else {
$email = input_data($_POST["email"]);
// check that the e-mail address is well-formed
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$emailErr = "Invalid email format";
}
}

//Number Validation
if (empty($_POST["mobileno"])) {
$mobilenoErr = "Mobile no is required";
} else {
$mobileno = input_data($_POST["mobileno"]);
// check if mobile no is well-formed
if (!preg_match ("/^[0-9]*$/", $mobileno) ) {
$mobilenoErr = "Only numeric value is allowed.";
}
//check mobile no length should not be less and greator than 10
if (strlen ($mobileno) != 10) {
$mobilenoErr = "Mobile no must contain 10 digits.";
}
}

//URL Validation
if (empty($_POST["website"])) {
$website = "";
} else {
$website = input_data($_POST["website"]);
// check if URL address syntax is valid
if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-
9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$website)) {
$websiteErr = "Invalid URL";
}
}
//Empty Field Validation
if (empty ($_POST["gender"])) {
$genderErr = "Gender is required";
} else {
$gender = input_data($_POST["gender"]);
}

//Checkbox Validation
if (!isset($_POST['agree'])){
$agreeErr = "Accept terms of services before submit.";
} else {
$agree = input_data($_POST["agree"]);
}
}
function input_data($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<h2>Registration Form</h2>
<span class = "error">* required field </span>
<br><br>
<form method="post" action="<?php echo htmlspecialchars($_SERVE
R["PHP_SELF"]); ?>" >
Name:
<input type="text" name="name">
<span class="error">* <?php echo $nameErr; ?> </span>
<br><br>
E-mail:
<input type="text" name="email">
<span class="error">* <?php echo $emailErr; ?> </span>
<br><br>
Mobile No:
<input type="text" name="mobileno">
<span class="error">* <?php echo $mobilenoErr; ?> </span>
<br><br>
Website:
<input type="text" name="website">
<span class="error"><?php echo $websiteErr; ?> </span>
<br><br>
Gender:
<input type="radio" name="gender" value="male"> Male
<input type="radio" name="gender" value="female"> Female
<input type="radio" name="gender" value="other"> Other
<span class="error">* <?php echo $genderErr; ?> </span>
<br><br>
Agree to Terms of Service:
<input type="checkbox" name="agree">
<span class="error">* <?php echo $agreeErr; ?> </span>
<br><br>
<input type="submit" name="submit" value="Submit">
<br><br>
</form>
<?php
if(isset($_POST['submit'])) {
if($nameErr == "" && $emailErr == "" && $mobilenoErr == "" &&
$genderErr == "" && $websiteErr == "" && $agreeErr == "") {
echo "<h3 color = #FF0001> <b>You have sucessfully registered.<
/b> </h3>";
echo "<h2>Your Input:</h2>";
echo "Name: " .$name;
echo "<br>";
echo "Email: " .$email;
echo "<br>";
echo "Mobile No: " .$mobileno;
echo "<br>";
echo "Website: " .$website;
echo "<br>";
echo "Gender: " .$gender;
} else {
echo "<h3> <b>You didn't filled up the form correctly.</b> </h3>"
;
}
}
?>
</body>
</html>

OUTPUT:-

You might also like