0% found this document useful (0 votes)
3 views6 pages

PHP Programs 2 Bca

The document contains multiple PHP scripts demonstrating various programming tasks, including finding the greatest of three numbers, printing patterns, calculating factorials, creating a student registration form, and performing string operations. It also includes scripts for setting and accessing cookies, printing associative and multidimensional arrays, connecting to MySQL, and creating a database and table. Each script is accompanied by HTML forms or output statements to illustrate functionality.

Uploaded by

deepam.sk2005
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)
3 views6 pages

PHP Programs 2 Bca

The document contains multiple PHP scripts demonstrating various programming tasks, including finding the greatest of three numbers, printing patterns, calculating factorials, creating a student registration form, and performing string operations. It also includes scripts for setting and accessing cookies, printing associative and multidimensional arrays, connecting to MySQL, and creating a database and table. Each script is accompanied by HTML forms or output statements to illustrate functionality.

Uploaded by

deepam.sk2005
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

23) Write a PHP script to find greatest of three numbers using function.

<?php
function checkNum($n1, $n2, $n3)
{
if ($n1 > $n2 && $n1 > $n3)
$temp = $n1;
elseif ($n2 > $n3)
$temp = $n2;
else
$temp = $n3;
return $temp;
}
$num1 = 11;
$num2 = 21;
$num3 = 3;
echo "Greatest among $num1 $num2 $num3 is " . checkNum($num1, $num2, $num3);
?>

24) Write a PHP script to print the following pattern


*
**
***
****
*****
<?php
$rows = 5;
for ($i = 1; $i <= $rows; $i++) {
for ($j = 1; $j <= $i; $j++) {
echo " * ";
}
echo "<br>";
}
?>

25) Write a PHP script to find factorial using recursion.


<?php
function fact ($n)
{
if($n <= 1)
{
return 1;
}
else
{
return $n * fact($n - 1);
}
}
echo "Factorial of 7 is " .fact(7);
?>

26) Write a program to create a web application to register a student without


database.

<html>
<head>
<title>
STUDENT RESGISTRATION FORM
</title>
</head>
<body>
<form method ="POST" action ="[Link]">
NAME: <input type= "text" name ="txtname"> <br>
AGE : <input type = "number" name ="txtage" min ="18" max="25"> <br>
GENDER : <input type="radio" name="gender" id="male" value ="MALE" > <label for
="male"> MALE </label>

<input type="radio" name="gender" id="female" value ="FEMALE" > <label for


="female"> FEMALE </label><BR>
<input type="submit" name ="submit" value="REGISTER">
</form>
</body>
</html>

[Link]
<?php
if($_POST)
{
echo " NAME: " .$_POST['txtname'] . "<br>";
echo "AGE: " .$_POST['txtage'] ."<br>";
echo "GENDER: " .$_POST['gender'] ."<br>";
}
?>

27) Write a menu driven PHP program to demonstrate string functions.

<html>
<head>
<title>
Menu driven program to demonstrate string functions </title>
</head>
<body>
<form method="POST">
Enter string: <input type="text" name ="str"> <br>
<h3>operations</h3>
1 <input type ="radio" name="choice" value ="1"> LENGTH OF STRING <br>
2 <input type ="radio" name="choice" value ="2"> REVERSE OF STRING <br>
3 <input type ="radio" name="choice" value ="3"> TOTAL WORDS <br>
4 <input type ="radio" name="choice" value ="4"> FIRST LETTER OF ALL WORDS TO
UPPERCASE <br>
5 <input type ="radio" name="choice" value ="5"> UPPERCASE <br>
6 <input type ="radio" name="choice" value ="6"> LOWERCASE<br>
7 <input type ="radio" name="choice" value ="7"> FIRST LETTER OF SENTENCE TO
UPPERCASE <br>
<input type = "submit" value = "ok">
<input type= "reset" value="cancel"> <br> <br>
</form>
<?php
if($_POST) {
$s= $_POST['str'];
$ch = $_POST['choice'];
switch($ch)
{
case 1: echo " Length is : " .strlen($s);
break;
case 2: echo "Reverse of string is : " .strrev($s);
break;
case 3: echo "Number of words is: " .str_word_count($s);
break;
case 4: echo " First letters of each words to uppercase : " .ucwords($s);
break;
case 5: echo " to uppercase:" .strtoupper($s);
break;
case 6: echo " to lowercase: " .strtolower($s);
break;
case 7: echo " First letter of sentence to uppercase :" .ucfirst($s);
break;
default: echo "choose an option !!!";
break;
}
}
?>
</body>
</html>
28) Write a PHP script to set a cookie and access it.
<?php
setcookie("user", "Sony");
?>
<html>
<body>
<?php
if(!isset($_COOKIE["user"])) {
echo "Sorry, cookie is not found!";
} else {
echo "<br/>Cookie Value: " . $_COOKIE["user"];
}
?>
</body>
</html>

29) Write a PHP script to print one for 1, two for 2, three for 3 using switch-case statement.
<?php
$digit=3;
switch($digit) {
case 1:
echo " 1 is one ";
break;
case 2:
echo "2 is two" ;
break;
case 3:
echo "3 is three";
break;
default:
echo " enter 1, 2 or 3";
}
?>

30) Write a PHP script to print a given associative array.


<?php
$age = array("Sonu" => "35", "Monu" => "25", "ashu" => "50", "raghu" => "20");
foreach($age as $k=>$v)
{
echo "Key : $k, Value :$v <br>";
}
?>

31) Write a PHP script to print a given multidimensional array.


<?php
$emp = array(array(101, "sonu", 35), array(102, "monu", 25), array(103, "ashu", 20),
array(104, "raghu", 40));
for($row=0; $row<4; $row++)
{
for($col=0; $col<3; $col++)
{
echo $emp[$row][$col] ." ";
}
echo "<br>";
}
?>

32) Write a PHP script to connect to MySQL and to create a database.

<?php
$servername ="localhost";
$username = "root";
$password = "";
$conn = mysqli_connect($servername, $username, $password);
if(mysqli_connect_errno())
{
die("connection failed" .mysqli_connect_error());
}
else
{
echo "connected successfully.....<br>";
}
$query = 'create database college';
if(mysqli_query($conn, $query)==TRUE)
echo "database created....";
else
die("failed" .mysqli_error($conn));
mysqli_close($conn);
?>

33) Write a PHP PROGRAM to create a table in MySQL.


<?php
$conn = mysqli_connect("localhost", "root", "", "college");
if(mysqli_connect_errno())
{
die("connection failed" .mysqli_connect_error());
}
else
{
echo "connected successfully.....<br>";
}
$query = 'create table student (id int PRIMARY KEY, name varchar(50) NOT NULL)';
if(mysqli_query($conn, $query)==TRUE)
echo "TABLE created....";
else
die("failed" .mysqli_error($conn));
mysqli_close($conn);
?>

You might also like