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

PHP Print Out

The document explains how to create PHP programs to demonstrate user-defined functions with and without arguments and return values. It includes examples of functions defined without and with arguments, without and with return values, and with a default argument. HTML forms are used to accept user input and display output. Function definitions and calls are written in PHP.

Uploaded by

ganga
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views

PHP Print Out

The document explains how to create PHP programs to demonstrate user-defined functions with and without arguments and return values. It includes examples of functions defined without and with arguments, without and with return values, and with a default argument. HTML forms are used to accept user input and display output. Function definitions and calls are written in PHP.

Uploaded by

ganga
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 40

PHP PROGRAM TO DEMONSTRATE ALL ARRAY OPERATIONS

(ARRAY_SEARCH(), ARRAY_DIFF(), ARRAY_COMBINE(), ARRAY_MATCH(), SORT())

arrays.html

<html>

<head>

<title>Array Functions</title>

</head>

<body>

<center><h1>Array functions using php</h1></center>

<form action="arrays.php">

Enter the first array elements:<textarea row="3" column="10" name="txt1">

</textarea><br>

Enter the second array elements:<textarea row="3" column="10" name="txt2">

</textarea> <br>

Enter the searching element:<input type="text" name="search"<br>

<input type="submit" value="submit">

</form>

</body>

</html>
arrays.php

<?php

$a1=explode(",",$_GET['txt1']);

array($a1);

print_r($a1);

$a2=explode(",",$_GET['txt2']);

array($a2);

print_r($a2);

echo "Array combine<br>";

$a3=array_combine($a1,$a2);

print_r($a3);

echo "Array difference<br>";

$a3=array_diff($a1,$a2);

print_r($a3);

echo "Array search<br>";

$s=$_GET['txtsearch'];

if(in_array($a3,$a1)
{
echo "Array element(".$s.")is found at the index of".array_search($s,$a1);

}
else
{
echo "Array element".$s."is not found at the index of".array_search($s,$a1);

echo "Arraysorting<br>";

echo "Ascending order of first array:";

sort($a1);

print_r($a1);

echo "Descending order of first array:";

rsort($a1);

print_r($a1);

?>
OUTPUT
PHP PROGRAM TO DEMONSTRATE ALL CONTROL STATEMENTS (FIND
FACTORIAL OF THE GIVEN NUMBER USING IF, WHILE, DO-WHILE.)

controlfact.html

<html>

<head>

<title>Factorial calculation</title>

</head>

<body>

<form action="controlfact.php">

<center><h1>Factorial</h1></center>

Enter any number:<input type="text" name="txtno">

<input type="radio" value="1" name="loop">while

<input type="radio" value="2" name="loop">do...while

<input type="radio" value="3" name="loop">for

<input type="submit" value="submit">

</form>

</body>

</html>
controlfact.php
<?php
$n=$_GET['txtno'];
$c=$_GET['loop'];
switch($c)
{
case 1:
echo "<h1> Factorial calculation using While loop </h1>";
$f=1;
$i=1;
while($i<=$n)
{
$f=$f*$i;
$i++;
}
echo "factorial of".$n."is:".$f;
break;
case 2:
echo "<h1>Factorial calculation using Do..While loop</h1>";
$f=1;
$i=1;
do
{
$f=$f *$i;
$i++;
}while($i<=$n);
echo "factorial of".$n."is:".$f;
break;
case 3:
echo "<h1>Factorial Calculation using For Loop</h1>";
$f=1;
for($i=1;$i<=$n;$i++)
{
$f=$f*$i;
}
echo "factorial of".$n."is:".$f;
break;
}
?>
OUTPUT
INVENTORY TABLE USING KEY AND VALUE PAIRS
<html >
<head>
<title>Untitled Document</title>
</head>
<body>

<?php
$Inventory = array (array('Wrist Watch',01,350), array('Cooler',12,1000),
array('Clutch',20,450) );
?>
<h2>Inventory Table</h2>
<table border="2" bordercolor="#000000">
<th>Sl.No</th>
<th>Product Name</th>
<th>Quantity</th>
<th>Product Per Price</th>
<?php
foreach ($Inventory as $key => $value)
{
echo "<tr>";
echo "<td>" . ++$key . "</td> <td>" . $value[0] . "</td>";
echo "<td>" . $value[1] . "</td>";
echo "<td>" . $value[2] . "</td>";
echo "</tr> ";
}
?>
</table>
</body>
</html>
STUDENT TABLE USING KEY AND VALUE PAIRS TO SEARCH PARTICULAR
STUDENT
<html>
<head>
<title>Student Search</title>
</head>
<body>

<?php
$Students = array
(array('XXX',01,'Present'),
array('YYY',02,'Present'),
array('ZZZ',03,'Absent') );
?>

<h2>Student Attendance List</h2>

<table border="3" bordercolor="#000000">


<th>Sl.No</th>
<th>Student Name</th>
<th>Roll.No</th>
<th>Attendance</th>
<?php
foreach ($Students as $key => $value)
{
echo "<tr>";
echo "<td>" . ++$key . "</td> <td>" . $value[0] . "</td>";
echo "<td>" . $value[1] . "</td>";
echo "<td>" . $value[2] . "</td>";
echo "</tr>

";
}
?>
</table>
<br />
<br />

<h4 align="center"> OUTPUT</h4>


<form action="studentsearch.php" method="post">
Enter the Student Number to Search<input type="text" name="num">
<table border="3" bordercolor="#000000" align="center">
<th>Sl.No</th>
<th>Student Name</th>
<th>Roll.No</th>
<th>Attendance</th>
<?php
$number=$_POST['num'];
$key=0;
foreach($Students as $value)
{
if ($value[1]==$number)
{
echo "<tr>";
echo "<td>" . ++$key . "</td> <td>" . $value[0] . "</td>";
echo "<td>" . $value[1] . "</td>";
echo "<td>" . $value[2] . "</td>";
echo "</tr>";
}
}
?>
</table>
</form>
</body>
</html>
USER DEFINED FUNCTIONS

1. FUNCTIONS WITHOUT ARGUMENT AND WITHOUT RETRUN VALUE

funadd.html

<html>

<head>

<title>Funtion without Argument and without return value</title>

</head>

<body>

<center><h1>User defined functions </h1></center>

<form action="funadd.php" method="get">

Enter first value:<input type="text" name="fno"><br>

Enter second value:<input type="text" name="sno"><br>

<input type="submit" value="submit">

</form>

</body>

</html>

Fun

]
add.php

<?php

echo "Function without argument and without return value<br>";

function add()

$n1=$_GET['fno'];

$n2=$_GET['sno'];

$n3=$n1 + $n2;

echo $n1. " + " .$n2. " = " .$n3;

add();

?>
OUTPUT
USER DEFINED FUNCTIONS

2. FUNCTIONS WITHOUT ARGUMENT AND WITH RETURN VALUE

funsub.html

<html>

<head>

<title>Function without Argument and with return value</title>

</head>

<body>

<center><h1>User defined functions </h1></center>

<form action="funsub.php" method="get">

Enter first value:<input type="text" name="fno"><br>

Enter second value:<input type="text" name="sno"><br>

<input type="submit" value="submit">

</form>

</body>

</html>

funsub.php

<?php

echo "Function without argument and with return value<br>";

function sub()

$n1=$_GET['fno'];

$n2=$_GET['sno'];

$n3=$n1 - $n2;

$r=$n1."-".$n2."=".$n3;

return $r;

echo sub();

?>
OUTPUT
USER DEFINED FUNCTIONS

3. FUNCTIONS WITH ARGUMENT AND WITHOUT RETURN VALUE

funmul.html

<html>

<head>

<title>Funtion with Argument and without return value</title>

</head>

<body>

<center><h1>User defined functions </h1></center>

<form action="funmul.php" method="get">

Enter first value:<input type="text" name="txt1"><br>

Enter second value:<input type="text" name="txt2"><br>

<input type="submit" value="submit">

</form>

</body>

</html>

funmul.php

<?php

echo "Function with argument and without return value<br>";

$n1=$_GET['txt1'];

$n2=$_GET['txt2'];

function mul($a,$b)

$c=$a*$b;

echo $a."*".$b."=".$c;

mul($n1,$n2);

?>
OUTPUT
USER DEFINED FUNCTIONS

4. FUNCTION WITH ARGUMENT AND WITH RETURN VALUE

fundiv.html

<html>

<head>

<title>Funtion with Argument and with return value</title>

</head>

<body>

<center><h1>User defined functions </h1></center>

<form action="fundiv.php" method="get">

Enter first value:<input type="text" name="txt1"><br>

Enter second value:<input type="text" name="txt2"><br>

<input type="submit" value="submit">

</form>

</body>

</html>

fundiv.php

<?php

echo "Funtion with argument and with return value<br>";

$n1=$_GET['txt1'];

$n2=$_GET['txt2'];

function div($a,$b)

$c=$a/$b;

$r=$a."/".$b."=".$c;

return $r;

$ret=div($n1,$n2);

echo $ret;

?>
OUTPUT
USER DEFINED FUNCTIONS

5. FUNCTION WITH DEFALUT ARGUMENTS

fundef.html

<html>

<head>

<title>Funtion with Default arguments</title>

</head>

<body>

<center><h1>User defined functions </h1></center>

<form action="fundef.php" method="get">

Enter Table name:<input type="text" name="txt1"><br>

Enter Starting term:<input type="text" name="txt2"><br>

Enter Ending term:<input type="text" name="txt3"><br>

<input type="submit" value="submit">

</form>

</body>

</html>
fundef.php

<?php

$t=$_GET['txt1'];

$s=$_GET['txt2'];

$e=$_GET['txt3'];

function display($t,$s=1,$e=10)

echo "start :".$s. "Ending : ".$e;

for($i=$s;$i<=$e;$i++)

$a=$i*$t;

echo "<p align:center>".$i."x".$t."=".$a;

if($s!=null && $e!=null)

display($t,$s,$e);

else if($s != null)

display($t,$s);

else if($e != null)

display($t,$s,$e);

else

display($t);

?>
OUTPUT
PHP program to find factorial of the given number using recursion

fact.html

<html>

<head>

<title>Factorial</title>

</head>

<body>

<form action="fact.php">

Enter any number:<input type="text" name="txt1">

<input type="submit" value="click">

</form>

</body>

</html>

fact.php

<?php

$a=$_GET['txt1'];

function fact($n)

if($n<=1)

return 1;

else

return $n*fact($n-1);

echo "factorial of".$a."is".fact($a);

?>
OUTPUT
PHP Program to calculate nCr using include command to include the factorial function
ncr.html
<html>

<head>

<title>nCr Calculation</title>

</head>

<body>

<center><h1>nCr Calculation</h1></center>

<form action="ncr.php">

Enter n value:<input type="text" name="txt1">

Enter r value:<input type="text" name="txt2">

<input type="submit" value="nCr">

</form>

</body>

</html>

ncr.php

<?php
$n=$_GET['txt1'];
$r=$_GET['txt2'];
function factorial($number)
{
if($number<0)
{
return -1;

for($i=1;$i<=$number;$i++)
{
$result=$result*i;

}
}
function ncr($n,$r)
{
return factorial($n)/(factorial($r)*factorial($n-$r));

}
?>
PHP PROGRAM TO WORKING WITH COOKIE

CREATING COOKIE

cookie.html

<html>

<head>

<title>Creating Cookie</title>

</head>

<body>

<center><h3>CREATING COOKIE</h3></center>

<form action="cookie.php">

User name:<input type="text" name="txt1"><br>

Password:<input type="password" name="txt2"><br>

<input type="submit" value="create cookie">

</form>

</body>

</html>

cookie.php

<?php

echo "<h3>Cookie created for username & password</h3>";

$name=$_GET['txt1'];

$password=$_GET['txt2'];

setcookie("name",$name);

setcookie("pword",$password);

?>
OUTPUT
PHP PROGRAM TO WORKING WITH COOKIE

RETRIVING COOKIES

retrieve.php

<html>

<head>

<title>Retrive Cookie</title>

</head>

<body>

<?php

if(isset($_COOKIE["name"])&&isset($_COOKIE["pword"]))

echo "<h3>RETRIVING COOKIES</h3>";

echo "The Name:".$_COOKIE["name"];

echo "<br>";

echo "The Password:".$_COOKIE["pword"];

else

echo "Cookies Deleted";

?>

</body>

</html>
OUTPUT
TO PROCESS PERSONAL DETAILS USING FILE

WRITING THE FILE

file.html

<html>

<head>

<title>File</title>

</head>

<body>

<center><h2>WRITING THE FILE</h2></center>

<form action="file.php">

Filename:<input type="text" name="txt1"><br>

Contents:<textarea row="20" column="10" name="txt2">

</textarea><br>

<input type="submit" value="create">

</form>

</body>

</html>

file.php

<?php

$fname=$_GET['txt1'];

$contents=$_GET['txt2'];

$fp=fopen("$fname","w");

fwrite($fp,$contents);

fclose($fp);

echo "file created successfully";

?>
OUTPUT
TO PROCESS PERSONAL DETAILS USING FILE

READING THE FILE

read.html

<html>

<head>

<title>Reading the entire file</title>

</head>

<body>

<form method="post" action="read.php">

<center><h1>READING THE ENTIRE FILE</h1></center>

Enter File name:<input type="text" name="txtfname"><br>

<input type="submit" value="read">

</form>

</body>

</html>

read.php

<?php

$fname=$_POST['txtfname'];

$fp=fopen($fname,"r");

while(!(feof($fp)))

echo fgets($fp)."<br>";

fclose($fp);

?>
OUTPUT
TO DESIGN AN STUDENT MARK USING HTML FORM
AND PROCESS USING PHP
mark.html

<html>

<head>

<title>Student Marklist</title>

</head>

<body>

<center><h1>Student Mark Details</h1></center>

<form action="mark.php">

Student Name:<input type="text" name="txtname" required autofocus>


<br>

Roll No:<input type="text" name="txtno" required><br>

Enter Java Mark:<input type="text" name="java" required><br>

Enter Maths Mark:<input type="text" name="mat" required><br>

Enter English Mark:<input type="text" name="eng" required><br>

<input type="submit" value="result">

<input type="reset" value="cancel">

</form>

</body>

</html>
mark.php

<?php

$n=$_GET['txtname'];

$ro=$_GET['txtno'];

$j=$_GET['java'];

$m=$_GET['mat'];

$e=$_GET['eng'];

$t=$j+$m+$e;

$avg=$t/3;

if($j>=35&&$m>=35&&$e>=35)

$r="pass";

else

$r="fail";

echo "Studentname:".$n."<br>";

echo "Rollno:".$ro."<br>";

echo "Java mark:".$j."<br>";

echo "Maths mark:".$m."<br>";

echo "English mark:".$e."<br>";

echo "Average:".$avg."<br>";

echo "result:".$r."<br>";

?>
OUTPUT
PHP PROGRAM TO DISPLAY STUDENT DETAILS USING DATABASE

insert.html

<html>

<head>

<title>Database Connection</title>

</head>

<body>

<center><h1>Database Connection</h1></center>

<form method="post" action="insert.php">

<pre>

Sname:<input type="text" name="sname">

Dept:<input type="text" name="dept">

% of marks:<input type="text" name="mark">

<input type="submit" value="Insert">

</pre>

</form>

</body>

</html>
insert.php

<?php

$sname=$_POST['sname'];

$dept=$_POST['dept'];

$marks=$_POST['mark'];

$con=mysqli_connect("localhost","root","","college");

if(!$con)
{
die("Error : ". sqli_connect_error());

}
else
{
$sql="insert into secondcsc values('$sname','$dept','$marks')";

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

if($result)
{
echo "Record Inserted";
}
else
{
echo "Record not Inserted";
}
}
?>
OUTPUT
display.php

<?php

$con=mysqli_connect("localhost","root","","college");

$sql="select * from secondcsc";

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

$n=mysqli_num_rows($result);

if($n>0)
{
echo"<table>";

echo "<tr>";

echo "<th>Student name</th>";

echo "<th>Department</th>";

echo "<th>Marks</th>";

echo "</tr>";

while($row=mysqli_fetch_assoc($result))
{
echo "<tr>";

echo "<td>".$row['sname'];

echo "<td>".$row['dept'];

echo "<td>".$row['mark'];

echo "</tr>";

echo "</table>";

else

echo "No such records";

?>

OUTPUT

You might also like