PHP and MySQL Lab Programs
1) Write
a PHP program to display student name, father name, mother name and phone no?
<html>
<head>
<title>Practical-1</title>
</head>
<?php
echo
"<b>Hello</b>
<i>World</i>"."<br>";
echo "<b>Name:
Rajesh Kumar</b>"."<br>";
echo "<b>FName:
Kiran</b>"."<br>";
echo "<b>MName:
Prasanna</b>"."<br>";
echo "<b>Phone:
9966554454</b>"."<br>";
?>
<body>
</body>
</html>
Output:
Hello World
Name: Rajesh Kumar
FName: Kiran
MName: Prasanna
Phone: 9966554454
Name: Rajesh Kumar
FName: Kiran
MName: Prasanna
Phone: 9966554454
2) Write
a PHP program to display the today’s date and current time?
<?php
print strftime('%c');
echo "<br />";
print strftime('%d/%m/%Y');
echo "<br />";
print strftime('%A, %d %B - %Y');
echo "<br />";
echo "<b>Current Day, Date and Time
is:</b> " . date("D M d, Y G:i A");
?>
Output:
Mon
Feb 25 18:37:05 2019
25/02/2019
Monday, 25 February - 2019
Current Day, Date and Time is: Mon Feb 25, 2019 18:37 PM
25/02/2019
Monday, 25 February - 2019
Current Day, Date and Time is: Mon Feb 25, 2019 18:37 PM
Note:
The strftime( ) function is used to get a
local time/date format according to the localization settings.
Day :
%a - Short day abbreviation ( Sun through Sat)
%A - A full day name ( Sunday through
Saturday)
%d - Numeric day of the month with leading
zeros (01 to 31)
%e - Day of the month without leading zeros (1
to 31 )
%j - Day of the year, 3 digits with leading
zeros (001 to 366)
%u - ISO-8601 numeric day of the week 1 (for
Monday) through 7 (for Sunday)
Month :
%b - Abbreviated month name (Jan through Dec)
%B - Full month name (January through
December)
%h - Abbreviated month name (Jan through Dec)
%m - Numeric representation of the month in
two digits 01 (for January) through 12 (for December)
Year :
%C - Numeric representation of the century in
two digits %g - Two digit representation of the year going by ISO-8601:1988
standards %G - The full four-digit representation of the year %y - Numeric
representation of the year in two digits (Example: 99 for1999)
%Y - Four digit representation for the year Example:
2011
Time and Date Stamps :
%c - A specific date and time stamp based on
local
%D - Same as "%m/%d/%y"
%F - Same as "%Y-%m-%d" (usually
used in database datestamps)
Date:The format parameter of the date( ) function is
in fact a string that can contain multiple characters allowing you to
generate a date string containing various components of the date and time, like
day of the week, AM or PM, etc. Here are some the date-related formatting
characters that are commonly used in format string:
•
d - Represent day of the month; two digits with leading
zeros (01 or 31)
•
D - Represent day of the week in text as an abbreviation
(Mon to Sun)
•
m - Represent month in numbers with leading zeros (01 or 12)
•
M - Represent month in text, abbreviated (Jan to Dec)
•
y - Represent year in two digits (08 or 14)
•
Y - Represent year in four digits (2008 or 2014)
The parts of the date can be
separated by inserting other characters, like hyphens (-), dots (.), slashes (/), or spaces
to add additional visual formatting.
Similarly you can use the following
characters to format the time string:
•
h - Represent hour in 12-hour format with leading zeros (01
to 12)
•
H - Represent hour in in 24-hour format with leading zeros
(00 to 23)
•
i - Represent minutes with leading zeros (00 to 59)
•
s - Represent seconds with leading zeros (00 to 59)
•
a - Represent lowercase ante meridiem and post meridiem (am
or pm)
•
A - Represent uppercase Ante meridiem and Post meridiem (AM
or PM)
3)
Write a PHP Program to print multiplication table?
|
<?php
$tableno=7;
$upto=10;
for($i=1;$i<=$upto;++$i)
{
echo "$tableno * $i
=".$tableno * $i."<br/>";
}
?>
4) Write
a PHP program to calculate SUM of given numbers using function?
<?php
$val1=10;
$val2=25;
function sum($val1,$val2)
{
$total=$val1+$val2;
return $total;
}
echo "<b>Sum using Function :
</b>" . sum($val1,$val2);
$sum=$val1
+ $val2;
echo
"<br />";
echo "<b>Sum is with using
formula:</b> $sum";
?>
Output:
Sum using Function : 35
Sum is with using formula: 35
Sum is with using formula: 35
5) Write
a PHP program to implement the concept of Form?
File name: form1.php
<!DOCTYPE HTML>
<html>
<body>
<form action="form1a.php"
method="post">
Name: <input type="text"
name="name"><br>
FName: <input type="text"
name="fname"><br>
MName: <input type="text"
name="mname"><br>
Phone: <input type="text"
name="phno"><br>
E-mail: <input type="text"
name="email"><br>
<input type="submit">
</form>
</body>
</html>
File name: form1a.php
<html>
<body>
Applicant Name: <?php echo $_POST["name"]; ?> <br>
Father Name: <?php echo
$_POST["fname"]; ?> <br>
Mother Name: <?php echo
$_POST["mname"]; ?> <br>
Phone No: <?php echo
$_POST["phno"]; ?> <br>
email address is: <?php echo
$_POST["email"]; ?>
</body>
</html>
First execute the form1.php
Then it will display
the following output:
Enter the details, then click on the Submit
button these details will be submitted to 2nd program i.e., form1a.php. Next output
will be displayed.
Applicant Name: Rajesh Kumar
Father Name: Kiran Kumar
Mother Name: Prasanna
Phone No: 9985454545
email address is: abc@gmail.com
Father Name: Kiran Kumar
Mother Name: Prasanna
Phone No: 9985454545
email address is: abc@gmail.com
6)
Write a PHP and HTML
combined program to display number guessing?
<?php
//PHP and HTML
combined program to guess a number
$num_to_guess = 42;
if(!isset($_POST['guess'])) {
$message = "Welcome to the guessing
machine!";
} elseif (!is_numeric($_POST['guess'])) { //
is not numeric
$message = "I don’t understand that
response.";
} elseif ($_POST['guess'] == $num_to_guess) {
// matches!
$message = "Well done!";
} elseif ($_POST['guess'] > $num_to_guess)
{
$message = $_POST['guess']." is too big!
Try a smaller number.";
} elseif ($_POST['guess'] < $num_to_guess)
{
$message = $_POST['guess']." is too
small! Try a larger number.";
} else { // some other condition
$message = "I am terribly
confused.";
}
?>
<html>
<head>
<title>A PHP number guessing
script</title>
</head>
<body>
<form action="<?php echo
$_SERVER['PHP_SELF']; ?>" method="POST">
<p><label
for="guess">Type your guess here:</label><br/>
<input type="text"
is="guess" name="guess" /></p>
<button type="submit"
name="submit" value="submit">Submit</button>
</form>
</body>
</html>
Output
<html>
<head>
<title>Practical-5
Marksheet</title>
</head>
<body>
<fieldset>
<legend
align="center">Enter Your Name with Marks Detail</legend>
<table
width="250px" border="2" align="center">
<tr>
<td
align="right">Name</td>
<td><input
type="text" name="txtname"></td>
</tr>
<tr>
<td
align="right">Subject-1</td>
<td><input
type="text" name="txtsub1"></td>
</tr>
<tr>
<td
align="right">Subject-2</td>
<td><input
type="text" name="txtsub2"></td>
</tr>
<tr>
<td
align="right">Subject-3</td>
<td><input
type="text" name="txtsub3"></td>
</tr>
<tr>
<td
align="right">Subject-4</td>
<td><input
type="text" name="txtsub4"></td>
</tr>
<tr>
<td
colspan="2" align="center">
<input
type="submit" name="submit"
value="Submit"></td>
</tr>
</table>
</fieldset>
</form>
</body>
</html>
Output
2nd
program p5a.php
<?php
if(isset($_REQUEST['submit']))
{
$name=$_REQUEST['txtname'];
$sub1=$_REQUEST['txtsub1'];
$sub2=$_REQUEST['txtsub2'];
$sub3=$_REQUEST['txtsub3'];
$sub4=$_REQUEST['txtsub4'];
echo
"<b>Your Name is: </b>" . $name . "<br>";
echo
"Subject-1: " . $sub1 . "<br>"; echo "Subject-2:
" . $sub2 . "<br>"; echo "Subject-3: " . $sub3 .
"<br>"; echo "Subject-4: " . $sub4 .
"<br>";
$total=$sub1+$sub2+$sub3+$sub4;
echo "Total
Marks: " . $total . "<br>";
$per=$total/4;
echo
"Percentage: " . $per . "%<br>";
switch($per)
{
case $per<35:
echo "Grade:
F" . "<br>";
break;
case $per>=35
&& $per<=50: echo "Grade: D" . "<br>";
break;
case $per>50
&& $per<=60:
echo "Grade:
C" . "<br>";
break;
case $per>60
&& $per<=70:
echo "Grade:
B" . "<br>";
break;
case $per>70
&& $per<100:
echo "Grade:
A" . "<br>";
break;
default:
echo "Invalid....
or out of limit";
break;
}
}
Else
{
echo "Go
Back and Press Submit button";
}
?>
Output
Your
Name is: Mahesh
Your Marks Detail:
Subject-1: 65
Subject-2: 68
Subject-3: 62
Subject-4: 64
Total Marks: 259
Percentage: 64.75%
Grade: B
Your Marks Detail:
Subject-1: 65
Subject-2: 68
Subject-3: 62
Subject-4: 64
Total Marks: 259
Percentage: 64.75%
Grade: B
****
This article is a great article that I have seen in my PHP Programming career so far.
ReplyDeletewebsite development company in Surat Gujarat
Extremely useful information which you have shared here Php-MySQL Classes
ReplyDelete