Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
13 views

PHP Prac Programs

The document contains 20 questions each demonstrating a different PHP program or concept. The questions cover PHP basics like forms, conditionals, loops, functions, classes, strings, cookies, sessions, mail, and MySQL. Common PHP functions, classes, MySQL commands are used throughout the programs.

Uploaded by

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

PHP Prac Programs

The document contains 20 questions each demonstrating a different PHP program or concept. The questions cover PHP basics like forms, conditionals, loops, functions, classes, strings, cookies, sessions, mail, and MySQL. Common PHP functions, classes, MySQL commands are used throughout the programs.

Uploaded by

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

PHP PROGRAMS

q1.

<html>

<body>

<form method="post">

Length:<input type="text" name="len"><br>

Width:<input type="text" name="wid"><br>

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

</form>

<?php

if(isset($_POST['submit']))

$len=$_POST['len'];

$wid=$_POST['wid'];

echo "Area of Rectangle:".$len*$wid;

?>

</body>

</html>

q2.

<form method="post">

Marks:<input type="text" name="mark"><br>

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

</form>

<?php
if(isset($_POST['submit']))

$mark=$_POST['mark'];

if($mark>80)

$grade="dist";

else if($mark>=60 && $mark<=80)

$grade="1st class";

else if($mark>=40 && $mark<60)

$grade="2nd class";

else

$grade="fail";

echo "Mark is".$mark."And Grade Is".$grade;

?>

</body>

</html>

q3.

<html>
<body>

<?php

$day=3;

switch($day)

case 1:

echo "its monday!";

break;

case 2:

echo "its Tuesday!";

break;

case 3:

echo "its Wednesday!";

break;

case 4:

echo "its Thursday!";

break;

case 5:

echo "its Friday!";

break;

case 6:

echo "its Saturday!";


break;

case 7:

echo "its Sunday!";

break;

default:

echo "invalid";

?>

</body>

</html>

q4.

<html>

<body>

<?php

$month=3;

switch($month)

case 1:

echo "its Jan!";

break;

case 2:

echo "its Feb!";


break;

case 3:

echo "its March!";

break;

case 4:

echo "its April!";

break;

case 5:

echo "its May!";

break;

case 6:

echo "its June!";

break;

case 7:

echo "its July!";

break;

case 8:

echo "its Aug!";

break;

case 9:
echo "its Sept!";

break;

case 10:

echo "its Oct!";

break;

case 11:

echo "its Nov!";

break;

case 12:

echo "its Dec!";

break;

default:

echo "invalid";

?>

</body>

</html>

q5.

<html>

<body>

<?php
$num=5;

$fact=1;

for($i=1;$i<$num;$i++)

$fact+=$fact*$i;

echo"factorial is :".$fact;

?>

</body>

</html>

q6.

<html>

<body>

<?php

$arr=array(1,2,3,4,5);

$sum=0;

foreach($arr as $val)

$sum+=$val;

echo "sum is:",$sum;

?>

</body>

</html>

q7.
<html>

<body>

<?php

$num=2;

for($i=1;$i<=10;$i++)

echo $num*$i."<br>";

?>

</body>

</html>

q8.

<?php

//strlen

echo "length ".strlen("hello world!")."<br>";

//wordCount

echo "word count ".str_word_count("hello world!")."<br>";

//strReverse

echo "reverse ".strrev("hello world!")."<br>";

//position

echo "position ".strpos("hello world!","world")."<br>";

//replace
echo "replace ".str_replace("world","dolly","hello world!")."<br>";

//upper

echo "upperCase ".strtoupper("hello World!")."<br>";

//lower

echo "lowerCase ".strtolower("HELLO WORLD!")."<br>";

//ucwords

echo "ucWords ".ucwords("hello World!")."<br>";

//strcmp

echo "strcmp ".strcmp("hi World!","hello World!")."<br>";

?>

q9.

<html>

<body>

<?php

function area($r)

echo "area of circle is:".(3.14*$r*$r);

area(4);

?>

</body>

</html>
q10.

<html>

<body>

<?php

function sqaure($n)

echo "Sqaure of number:".$n*$n;

sqaure(4);

?>

</body>

</html>

q11.

<html>

<body>

<?php

class A

function hello()

echo "Hello !";

class B extends A

function hi()
{

echo "Hi !";

$b=new B();

$b->hello();

$b->hi();

?>

</body>

</html>

q12.

<?php

class A

function __construct()

echo "constructor called!";

$a=new A();

?>

q13.

<html>

<body>

<form method="post">
Name:<input type="text" name="name"><br><br>

Address:<textarea rows="5" cols="10" name="add"></textarea><br><br>

Gender:<input type="radio" name="gen" value="male">Male

<input type="radio" name="gen" value="female">Female<br><br>

Subject:<input type="checkbox" name="sub[]" value="php">PHP

<input type="checkbox" name="sub[]" value="mad">MAD<br><br>

Hobbies:<select name="hob">

<option value="reading">Reading</option>

<option value="study">Study</option>

</select><br><br>

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

</form>

</body>

</html>

q14.

<html>

<body>

<form method="post">

Hobbies:<select name="hob">

<option value="reading">Reading</option>

<option value="study">Study</option>

</select><br><br>

Subjects:<select name="sub[]">

<option value="English">English</option>

<option value="Marathi">Marathi</option>

<option value="Hindi">Hindi</option>
</select><br><br>

<input type="hidden" name="hid" value="hidden"><br>

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

</form>

</body>

</html>

q15.

<html>

<body>

<form method="post">

Name:<input type="text" name="name"><br><br>

Mob no:<input type="text" name="mob"><br><br>

Gender:<input type="radio" name="gen" value="male">Male<br><br>

<input type="radio" name="gen" value="female">Female<br><br>

Email:<input type="text" name="email"><br><br>

<input type="submit" name="submit" value="Submit"><br><br>

</form>

<?php

if(isset($_POST['submit']))

$name=$_POST['name'];

if(empty($name))

echo "enter name"."<br>";

else
{

if(preg_match("/^[a-zA-Z]*$/",$name))

echo $name."<br>";

$mob=$_POST['mob'];

if(empty($mob))

echo "enter mobile number";

else

if(preg_match("/^[0-9]{10}$/",$mob))

echo $mob."<br>";

else

echo "invalid mobile no!";

if(empty($_POST['gen']))

echo "select gender"."<br>";

}
else

$gen=$_POST['gen'];

echo "gender is".$gen."<br>";

$email=$_POST['email'];

if(empty($email))

echo "enter email"."<br>";

else

if(filter_var($email,FILTER_VALIDATE_EMAIL))

echo $email."<br>";

else

echo "invalid email"."<br>";

?>

</body>

</html>
q16.

cookie.php

<?php

setcookie("paradkar","tyco",time()+60,"/","",1);

?>

cookie2.php

<?php

echo $_COOKIE['paradkar'];

?>

q17.

//session start

<?php

session_start();

$_SESSION["name"]="indira";

echo $_SESSION["name"];

?>

//session destroy

<?php

session_start();

echo $_SESSION["name"];

session_destroy();

echo $_SESSION["name"];

?>

q18.
<html>

<body>

<?php

$to="indira56@gmail.com";

$subject="This is subject";

$message="<h1>This is html page</h1>";

$header="From:tyco12@gmail.com";

$retval=mail($to,$subject,$message,$header);

if($retval== true)

echo "message sent successful!";

else

echo "message not sent!";

?>

</body>

</html>

q19 and q20.

<?php

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

if($con)

echo "dbconnected"."<br>";

}
else

echo "error";

/*$sql="create database ty";

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

if($result)

echo "db created";

else

echo "error";

}*/

mysqli_select_db($con,"ty");

/*

$sql="create table anushka(name varchar(20),age int(10))";

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

if($result)

echo "table created";

else

echo "error";

}*/
/*$sql="insert into anushka values('indira',18)";

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

if($result)

echo "data inserted";

else

echo "data not inserted";

*/

/*$sql="update anushka set age=100";

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

if($result)

echo "data inserted";

else

echo "data not inserted";

}*/

/*$sql="delete from anushka where age=100";

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

if($result)

{
echo "data deleted";

else

echo "data not deleted";

}*/

$sql="select * from anushka";

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

while($row=mysqli_fetch_array($result))

echo $row['name'];

echo $row['age'];

echo "<br>";

?>

You might also like