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

Practical for PHP Programming

Computer science ,bca
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)
225 views

Practical for PHP Programming

Computer science ,bca
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/ 20

Practical Assignments for PHP Programming

1. Write a PHP script to print “hello world”.


2. Write a PHPscript to find odd or even number from given number.
3. Write a PHPscript to find maximum of three numbers.
4. Write a PHPscript to swap two numbers.
5. Write a PHPscript to find the factorial of a number.
6. Write a PHPscript to check whether given number is palindrome or not.
7. Write a PHP script to reverse a given number and calculate its sum
8. Write a PHP script to to generate a Fibonacci series using Recursive function
9. Write a PHP script to implement atleast seven string functions.
10. Write a PHP program to insert new item in array on any position in PHP.
11. Write a PHP script to implement constructor and destructor
12. Write a PHP script to implement form handling using get method
13. Write a PHP script to implement form handling using post method.
14. Write a PHP script that receive form input by the method post to check the number is prime
or not
15. Write a PHP script that receive string as a form input
16. Write a PHP script to compute addition of two matrices as a form input.
17. Write a PHP script to show the functionality of date and time function.
18. Write a PHP program to upload a file
19. Write a PHP script to implement database creation
20. Write a PHP script to create table
III BCA VI SEM: PHP & MySQL 2

1.Write a PHP script to print “hello world”

<?php
echo "Hello World!";
?>

Output

2. Write a PHPscript to find odd or even number from given number.


<?php
$num=6;
if($num%2==0)
{
echo "Give Number is EVEN Number";
}
else
{
echo "Give Number is ODD Number";
}
?>

Output
III BCA VI SEM: PHP & MySQL 3

3. Write a PHPscript to find maximum of three numbers.


<?php
$a=3;
$b=4;
$c=2;
if($a>$b && $a>$c)
{
echo "A is Biggest Number";
}
else if($b>$c)
{
echo "B is Biggest Number";
}
else
{
echo "C is Biggest Number";
}
?>

Output

4. Write a PHPscript to swap two numbers.


<?php
$a=4;
$b=6;
echo "Before Swapping Value of A is $a and Value of B is $b</br>";
$a=$a+$b;
$b=$a-$b;
$a=$a-$b;
echo "After Swapping Value of A is $a and Value of B is $b";
?>
Output
III BCA VI SEM: PHP & MySQL 4

5. Write a PHPscript to find the factorial of a number.


<?php
$n=4;
$f=1;
for($i=1;$i<=$n;$i++)
{
$f*=$i;
}
echo "Factorial of the Given Number is $f";
?>
Output

6. Write a PHPscript to check whether given number is palindrome or not.


<?php
$num=123;
$rev=0;
$temp=$num;
while($num>1)
{
$r=$num%10;
$rev=$rev*10+$r;
$num=$num/10;
}
if($rev==$temp)
{
echo "The Given Number is Polidrome";
}
else
{
echo "The Give Number is Not a Polidrome";
}
?>

Output
III BCA VI SEM: PHP & MySQL 5

7. Write a PHP script to reverse a given number and calculate its sum
<?php
$num=123;
$rev=0;
$sum=0;
while($num>1)
{
$r=$num%10;
$sum=$sum+$r;
$rev=$rev*10+$r;
$num=$num/10;
}
echo "The Reverse of the Given Number is $rev </br>";
echo " The Sum of its number is $sum";
?>
Output

8. Write a PHP script to to generate a Fibonacci series using Recursive function


<?php
$num = 5;
echo "Fibonacci series using recursive function:</br>";
function fibo($num)
{
if($num == 0)
{
return 0;
}
else if( $num == 1)
{
return 1;
}
else
{
III BCA VI SEM: PHP & MySQL 6

return (fibo($num-1) + fibo($num-2));


}
}

for ($i = 0; $i < $num; $i++){


echo fibo($i);
echo "</br>";
}
Output

9. Write a PHP script to implement atleast seven string functions.


<?php
$str="jss college for women chamarajanagar";
$str1=$str;
$str=strtoupper($str);
echo $str."</br>";
$str=strtolower($str);
echo $str."</br>";
$str=ucfirst($str1);
echo $str."</br>";
$str=ucwords($str1);
echo $str."</br>";
$str=strrev($str1);
echo $str."</br>";
$str=strlen($str1);
echo $str."</br>";
$str=lcfirst($str1);
echo $str."</br>";
?>
III BCA VI SEM: PHP & MySQL 7

Output

10. Write a PHP program to insert new item in array on any position in PHP.
<?php
$original_array = array( 2,3,4,4,5,6,7);
echo 'Original array : '."</br>";
foreach ($original_array as $x)
{
echo "$x "."</br>";
}
$ins_value = '9';
$pos = 3;
array_splice( $original_array, $pos, 0, $ins_value );
echo "After inserting 9 in the array is : "."</br>";
foreach ($original_array as $x)
{
echo "$x "."</br>";
}
?>
Output
III BCA VI SEM: PHP & MySQL 8

11. Write a PHP script to implement constructor and destructor


<?php
class Person
{
private $fname;
private $lname;
public function construct($fname, $lname) {
echo "Initialising the object...<br/>";
$this->fname = $fname;
$this->lname = $lname;
}
public function destruct(){
echo "Destroying Object...";
}
public function showName() {
echo "My name is: " . $this->fname . " " . $this->lname
. "<br/>";
}
}
$mallesha = new Person("Mallesha", "Revanna");
$mallesha->showName();
?>
Output
III BCA VI SEM: PHP & MySQL 9

12. Write a PHP script to implement form handling using get method

<html>
<head>
<title>12 Program </title>
</head>
<body>
<form action="12P.php" method="GET">
<table border="1px" align="center">
<tr>
<td colspan="2" align="center"> Login Page</td>
</tr>
<tr>
<td>Name</td>
<td><input type="text" name="Name"></td>
</tr>
<tr>
<td>Passowrd</td>
<td><input type="password" name="pass"></td>
</tr>
<tr>
<td></td>
<td>
<input type="submit" name="submit">
<input type="reset" value="reset">
</td>
</tr>
</table>
</form>
</body>
</html>
<?php

if(isset($_GET['submit']))
{
$name=$_GET['Name'];
$pass=$_GET['pass'];
echo "Your Name is $name </br>";
echo "Your Password is $pass</br>";
}
?>

Output
III BCA VI SEM: PHP & MySQL 10

13. Write a PHP script to implement form handling using post method

<html>
<head>
<title>13 Program </title>
</head>
<body>
<form action="13P.php" method="POST">
<table align="center" border="1px">
<tr>
<td colspan="2" align="center"> Login Page</td>
</tr>
<tr>
<td>Name</td>
<td><input type="text" name="Name"></td>
</tr>
<tr>
<td>Passowrd</td>
<td><input type="password" name="pass"></td>
</tr>
<tr>
<td>

</td>
<td>
<input type="submit" name="submit">
<input type="reset" value="reset">
</td>
</tr>
</table>
</form>
</body>
</html>
<?php

if(isset($_POST['submit']))
{
$name=$_POST['Name'];
$pass=$_POST['pass'];
echo "Your Name is $name </br>";
echo "Your Password is $pass</br>";
III BCA VI SEM: PHP & MySQL 11

}
?>
Output

14. Write a PHP script that receive form input by the method post to check the number is
prime or not
<html>
<head>
<title>12 Program </title>
</head>
<body>
<form action="14P.php" method="POST">
<table align="center" border="1px">
<tr>
<td colspan="2" align="center">Check Prime</td>
</tr>
<tr>
<td>Enter Number</td>
<td><input type="text" name="Number"></td>
</tr>

<tr>
<td>

</td>
<td>
<input type="submit" value="submit" name="submit">
</td>
</tr>
</table>
</form>
</body>
</html>
<?php

if(isset($_POST['submit']))
{
$flag=0;
III BCA VI SEM: PHP & MySQL 12

$num=$_POST['Number'];
for($i=2;$i<$num;$i++)
{
if($num % $i == 0)
{
$flag=1;
break;
}
}
if($flag==1)
{
echo "The Given Number is not Prime";
}
else
{
echo "The Given Number is Prime";
}
}
?>
Output

15. Write a PHP script that receive string as a form input


<html>
<head>
<title>
String as input Form</title>
<style>
th
{
background-color:black;
color:white;
}
</style>
</head>
<body>
<form method="post" action="15P.php">
<table border="01" align="center">
III BCA VI SEM: PHP & MySQL 13

<tr>
<th colspan="2">String as Input </th>
</tr>
<tr>
<td>Name:
</td>
<td>
<input type="text" name="Name" placeholder="Enter Your Name" >
</td>
</tr>
<tr>
<td>
</td>
<td>
<input type="submit" value="Submit" name="Submit">
</td>
</table>
</form>
</body>
</html>
<?php
if(isset($_POST['Submit']))
{
$name = $_POST['Name'];
echo "<script>alert(' Your Name is $name')</script>";
}
?>
Output
III BCA VI SEM: PHP & MySQL 14

16. Write a PHP script to compute addition of two matrices as a form Input
<html>
<head>
<title>Program 16</title>
</head>
<body>
<form action="16P.php" method="POST">
<h1 align="center "> Matrix Additon</h1>
<hr color="red">
<table>
<tr><td><h4>Matrix A</h4></td></tr>
<tr>
<td> Enter [0,0]</td>
<td><input type="text" name="M100" required></td>
</tr>
<tr>
<td> Enter [0,1]</td>
<td><input type="text" name="M101" required></td>
</tr>
<tr>
<td> Enter [1,0]</td>
<td><input type="text" name="M110" required></td>
</tr>
<tr>
<td> Enter [1,1]</td>
<td><input type="text" name="M111" required></td>
</tr>

<tr><td><h4>Matrix B</h4></td></tr>
<tr>
<td> Enter [0,0]</td>
<td><input type="text" name="M200" required></td>
</tr>
<tr>
<td> Enter [0,1]</td>
<td><input type="text" name="M201" required></td>
</tr>
<tr>
<td> Enter [1,0]</td>
<td><input type="text" name="M210" required></td>
</tr>
<tr>
<td> Enter [1,1]</td>
<td><input type="text" name="M211" required></td>
</tr>
<tr>
<td></td>
<td>
<input type="submit" name="add" >
III BCA VI SEM: PHP & MySQL 15

</td>
</tr>
</table>

</form>
</body>
</html>
<?php
if(isset($_POST['add']))
{
$M100=$_POST["M100"];
$M101=$_POST["M101"];
$M110=$_POST["M110"];
$M111=$_POST["M111"];
$M200=$_POST["M200"];
$M201=$_POST["M201"];
$M210=$_POST["M210"];
$M211=$_POST["M211"];
$matrix1=array(array($M100,$M101),array($M110,$M111));
$matrix2=array(array($M200,$M201),array($M210,$M211));
$m=count($matrix1);
$n=count($matrix1[0]);
echo "<hr color='blue'>";
echo "<h2> Matrix A Is </h2>"."\n";
for($i=0;$i<$n;$i++)
{
for($j=0;$j<$m;$j++)
{
echo $matrix1[$i][$j]." ";
}
echo "</br>";
}

echo "<hr color='blue'>";


echo "<h2> Matrix B Is</h2>"."\n";
for($i=0;$i<$n;$i++)
{
for($j=0;$j<$m;$j++)
{
echo $matrix2[$i][$j]." ";
}
echo "</br>";
}
echo "<hr color='blue'>";
echo "<h2> Addition of Two Matrix </h2>"."\n";
for($i=0;$i<$n;$i++)
{
for($j=0;$j<$m;$j++)
III BCA VI SEM: PHP & MySQL 16

{
$add[$i][$j]=$matrix1[$i][$j]+$matrix2[$i][$j];
}
}
for($i=0;$i<$n;$i++)
{
for($j=0;$j<$m;$j++)
{
echo $add[$i][$j]." ";
}
echo "</br>";
}
}
?>
Output

17. Write a PHP script to show the functionality of date and time function.
<?php
//formats today's date in three different ways:
echo "Today's date in various formats:" . "</br>";
echo "Today is " . date("Y/m/d") . "<br>";
echo "Today is " . date("Y.m.d") . "<br>";
echo "Today is " . date("Y-m-d") . "<br>";
echo "Today is " . date("l"). "<br>";
III BCA VI SEM: PHP & MySQL 17

//the current time in the specified format:


date_default_timezone_set("Asia/Calcutta"). "<br>";
echo "The time is " . date("h:i:sa"). "<br>";
$d=mktime(11, 14, 54, 8, 12, 2014);
echo "Created date is " . date("Y-m-d h:i:sa", $d). "<br>";
$d=strtotime("tomorrow");
echo date("Y-m-d h:i:sa", $d) . "<br>";
//outputs the dates for the next six Saturdays:
$startdate = strtotime("Saturday");
$enddate = strtotime("+6 weeks", $startdate);
while ($startdate < $enddate)
{
echo date("M d", $startdate) . "<br>";
$startdate = strtotime("+1 week", $startdate);
}
?>
Output

18. Write a PHP program to upload a file


<?php
if(isset($_FILES['fileToUpload']))
{
$target_path = "D:\php_files/";
$target_path = $target_path.basename(
$_FILES['fileToUpload']['name']);

if(move_uploaded_file($_FILES['fileToUpload']['tmp_name'],
$target_path))
{
III BCA VI SEM: PHP & MySQL 18

echo "File uploaded successfully!";


} else
{
echo "Sorry, file not uploaded, please try again!";
}
}
?>
<html>
<body>

<form action="" method="POST" enctype="multipart/form-data">


<input type="file" name="fileToUpload" />
<input type="submit"/>
</form>

</body>
</html>
Output

19. Write a PHP script to implement database Creation


<?php
$host = "localhost";;
$user = "root";
$pass = "mallesha";
$conn = mysqli_connect($host, $user, $pass);
if(! $conn )
{
die('Could not connect: ' . mysqli_connect_error());
}
echo 'Connected successfully<br/>';

$sql = 'CREATE Database JSSCollege';


if(mysqli_query( $conn,$sql)){
echo "Database JSSCollege created successfully.";
III BCA VI SEM: PHP & MySQL 19

}else{
echo "Sorry, database creation failed ".mysqli_error($conn);
}
mysqli_close($conn);
?>
Output

20. Write a PHP script to create table


<?php
$host = "localhost";
$user = "root";
$pass = "mallesha";
$dbname ="JSSCollege";

$conn = mysqli_connect($host, $user, $pass,$dbname);


if(!$conn){
die('Could not connect: '.mysqli_connect_error());
}
echo 'Connected successfully<br/>';

$sql = "create table student(Admission_Number INT AUTO_INCREMENT,name


VARCHAR(20) NOT NULL, Course VARCHAR(20) NOT NULL,Gender varchar(10)
not null,Ph_number varchar(20),E_Mail varchar(20), Address
varchar(40),primary key (Admission_Number))";
if(mysqli_query($conn, $sql)){
III BCA VI SEM: PHP & MySQL 20

echo "Table Student created successfully";


}else{
echo "Could not create table: ". mysqli_error($conn);
}
mysqli_close($conn);
?>
Output

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