0% found this document useful (0 votes)
41 views41 pages

PHP P4

The document contains 10 programming exercises with their aims, algorithms, programs, outputs, and results. Exercise 9 aims to write a PHP script to calculate the difference between two dates. The algorithm is to use the date_create() function to create two dates, then use date_diff() to get the difference between them and print it. The program defines two dates with date_create(), uses date_diff() to get the interval between them, then prints the result.

Uploaded by

Chellam
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)
41 views41 pages

PHP P4

The document contains 10 programming exercises with their aims, algorithms, programs, outputs, and results. Exercise 9 aims to write a PHP script to calculate the difference between two dates. The algorithm is to use the date_create() function to create two dates, then use date_diff() to get the difference between them and print it. The program defines two dates with date_create(), uses date_diff() to get the interval between them, then prints the result.

Uploaded by

Chellam
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/ 41

INDEX

PAGE
S.NO. DATE NAME OF THE PROGRAM SIGNATURE
NO.
SIMPLE HTML FORM TO CREATE A
1
USERNAME
PHP SCRIPT TO REDIRECT A USER TO A
2
DIFFERENT PAGE

3 USE OF TERNARY OPERATOR

SORT FUNCTIONS USAGE WITH ARRAY


4
CONCEPT
LOWEST AND HIGHEST TEMPERATURE
5
DISPLAY

6 SUM OF NUMBERS BETWEEN 0 TO 30

7 CREATE A CHESS BOARD

8 TO CHECK THE LOWERCASE IN A STRING

9 DIFFERENCE BETWEEN TWO DATES

10 DISPLAY OF TIME IN A SPECIFIED ZONE


EX NO: 01

DATE:

SIMPLE HTML FORM TO CREATE A USERNAME

AIM:

To write a simple HTML form and accept the username and display the name
through PHP echo statement.

ALGORITHM:

1. Create a HTML PHP login form.

2. Open the body tag.

3. Inside the form tag use port method.

4. Close the form tag.

5. Open the PHP script.

6. Use the command $name=$_POST[‘name’]

7. Close the PHP script and HTML tag.


PROGRAM:
<html>

<head>

<title>Student Name</title>

<meta http-equiv="content-type" content="text/html";charset=UTF-8;

</head>

<body>

<form method='post'>

<h2>Please enter your name:</h2>

<input type="text" name="name">

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

</form>

<?php

$name=$_POST['name'];

echo"<h3>Hello $name</h3>";

?>

</body>

</html>
OUTPUT:
RESULT:
Thus the program to accept the username and display the name through PHP
echo statement was executed and verified successfully.
EX NO: 02

DATE:

PHP SCRIPT TO REDIRECT A USER TO A DIFFERENT PAGE

AIM:

To write a PHP script to redirect a user to a different page.

ALGORITHM:

1. Start the program.

2. Open the HTML tag.

3. Open the body tag, inside the body tag open the PHP script.

4. Use the command header(‘location:http://localhost:8080//ex.02.php’)

5. Close the PHP script and HTML tag.


PROGRAM:
<html>

<body>

<?php

header('Location:http://localhost:8080/ex01.php');

?>

</body>

</html>
OUTPUT:
RESULT:
Thus the program to redirect a user to a different page was executed and
verified successfully.
EX.NO: 03

DATE:

USE OF TERNARY OPERATOR

AIM:

To write a PHP function to test whether a number is greater than 30, 20, 10
using ternary operator.

ALGORITHM:

1. Open the HTML tag.

2. Open the body tag, inside the body tag open the PHP script.

3. Write the function, using ternary operator.

4. Check whether the given number is greater than 30, 20, 10

5. Write the echo statement for the given ternary operator.

6. Test given functions for ternary_test()

7. Close the PHP script and HTML tag.


PROGRAM:
<html>
<body>
<?php
function ternary_Test($n)
{
$r=$n>30
?"is greater than 30</br>"
:($n>20
?"is greater than 20</br>"
:($n>10
?"is greater than 10</br>"
:"input a number atleast greater than 10!</br>"));
echo $n.":".$r."\n";
}
ternary_Test(32);
ternary_Test(21);
ternary_Test(12);
ternary_Test(4);
?>
</body>
</html>
OUTPUT:
RESULT:
Thus the program to test a number is greater than 30, 20, 10 using
ternary operator was executed and verified successfully.
EX NO: 04

DATE:

SORT FUNCTIONS USAGE WITH ARRAY CONCEPT

AIM:

To write a PHP script which displays the capital and its country name from the
given array in a sorted array.

ALGORITHM:

1. Open the HTML tag.

2. Open the body tag,inside the body tag open the PHP script.

3. Create an array with variable $city for display the capital and country name.

4. Then use a sort method ($city).

5. Use each command for displaying the city and capital.

6. Close the PHP script and HTML tag.


PROGRAM:
<html>

<body>

<?php

$city =array("Newdelhi"=>"India","Pairs"=>"France",

"London"=> "UK","Washingtgon"=>"US",

"Colombo"=>"Srilannka","Beijing"=>"China",

"Tokyo"=>"Japan","kabul"=>"Afghanistan",

"Lusaka"=>"Zambia","Mascat"=>"Oman");

asort($city);

foreach($city as $capital=>$country)

echo"The capital of $country is $capital<br>";

?>

</body>

</html>
OUTPUT:
RESULT:
Thus the program to display the capital and its name in a sorted form was
executed and verified successfully.
EX NO: 05

DATE:

LOWEST AND HIGHEST TEMPERATURE DISPLAY

AIM:

To write a PHP script to calculate and display average temperature, 5 lowest


and highest temperature.

ALGORITHM:

1. Start the program.

2. Open the HTML tag.

3. Open the PHP tag.

4. Give some values for temperature.

5. Find the total of temperature value,and then find its average.

6. Total is calculated.

7. Sort the temperature value.

8. Find the lowest 5 value using for loop.

9. Display the result.

10. Find the highest 5 value using for loop.

11. Display the final resultant value.

12. Then close the PHP tag.

13. Finally close the HTML tag.

14. End the program.


PROGRAM :
<html>
<body>
<?php
$month_temp = "73,25,91,35,16,48,23,58,15,92,72,56,38,49,28";
$temp_array = explode(',', $month_temp);
$tot_temp = 0;
$temp_array_length = count($temp_array);
foreach($temp_array as $temp)
{
$tot_temp += $temp;
}
$avg_high_temp = $tot_temp/$temp_array_length;
echo "Average Temperature is : ".$avg_high_temp." ";
sort($temp_array);
echo " </br> List of five lowest temperatures :";
for ($i=0; $i< 5; $i++)
{
echo $temp_array[$i].", ";
}
echo "</br>List of five highest temperatures :";
for ($i=($temp_array_length-5); $i< ($temp_array_length); $i++)
{
echo $temp_array[$i].", ";
}
?>
</body>
</html>
OUTPUT:
RESULT:
Thus the program to display the average temperature, 5 lowest temperature
and highest temperature was executed and verified successfully.
EX NO: 06

DATE:

SUM OF NUMBERS BETWEEN 0 AND 30

AIM:

To write a PHP script using ‘for’ loop to add all the integers between 0
and 30 and display the total.

ALGORITHM:

1. Start the program.

2. Open the HTML tag.

3. Then open the PHP tag.

4. Initialize sum=0

5. Initialize the value=0 from for loop.

6. If $x=0;-Initialize the loop counter ($x), and set the start value to 0.

7. If $x<=10;-Continue the loop as long as $x is less than or equal to 10.

8. If $x++ - Increase the loop counter value by 1 for each iteration.

9. Increase the value one by one.

10. If the conditions is true, it will go to for loop.

11. Add the sum and initial value.

12. Finally print the sum.

13. Close the PHP tag.

14. Finally close the HTML tag.

15. End the program


PROGRAM :
<html>

<body>

<h2>SUM OF NUMBERS BETWEEN 0 TO 30</h2>

<?php

$sum = 0;

for($x=1; $x<=30; $x++)

$sum +=$x;

echo "The sum of the numbers 0 to 30 is $sum"."\n";

?>

</body>

</html>
OUTPUT:
RESULT:
Thus the program to add all the integers between 0 and 30, and displaying its
total was executed and verified successfully.
EX NO: 07

DATE:

CREATE A CHESS BOARD

AIM:

To write a PHP script using for loop that creates a chess board.

ALGORITHM:

1. Start the program.

2. Open the HTML tag.

3. Open the PHP tag.

4. To create the chess in PHP two loops are needed and each will create 8
blocks.

5. The inner-loop will generate table row with black and white background-color
based on the value.

6. If the value is even, black background is generated.

7. If the value is odd, white background is generated.

8. Then close the PHP tag.

9. Finally close the HTML tag.

10 .End the program.


PROGRAM :
<html>
<head>
<title>CHESS BOARD</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<h3>Chess Board using Nested For Loop</h3>
<table width="270px" cellspacing="0px" cellpadding="0px" border="1px">
<!-- cell 270px wide (8 columns x 60px) -->
<?php
for($row=1;$row<=8;$row++)
{
echo "<tr>";
for($col=1;$col<=8;$col++)
{
$total=$row+$col;
if($total%2==0)
{
echo "<td height=30px width=30px bgcolor=#FFFFFF></td>";
}
else
{
echo "<td height=30px width=30px bgcolor=#000000></td>";
}
}
echo "</tr>";
}
?>
</table>
</body>
</html>
OUTPUT:
RESULT:
Thus the program to create a chess board using for loop was executed and
verified successfully.
EX NO:08

DATE:

TO CHECK THE LOWERCASE IN A STRING

AIM:

To write a PHP function that checks if a string is all lower case character or not.

ALGORITHM:

1. Start the program.

2. Open the HTML tag.

3. Open the body tag, inside the body tag open the PHP script.

4. Assign a string value to the variable.

5. Check the type of the string.

6. If it is lowercase string then display the result, else display the result as not

in lowercase letters.

7. Stop.
PROGRAM :
<html>

<body>

<?php

$string = "india";

if (ctype_lower($string))

echo $string . ' is all lowercase letters.';

else

echo $string . ' is not all lowercase letters.';

?>

</body>

</html>
OUTPUT:
RESULT:

Thus the program to check the string contains all the characters are lowercase
or not.
EX NO:09

DATE:
DIFFERENCES BETWEEN TWO DATES

AIM:

To write a PHP script to calculate the differences between two dates.

ALGORITHM:

1. Start the program.

2. Open the HTML tag.

3. Open the body tag,inside the body tag open the PHP script.

4. Use date_create() function.

5. Write two different dates inside a parenthesis of date_create() function.

6. Use date_diff() function to get difference.

7. Print the year,month,day.

8. Close the PHP script and the body tag.

9. Close the HTML tag.

10 .End the program.


PROGRAM :
<html>

<body>

<h2>Difference betwen two dates</h2>

<?php

$date1 = date_create( "2022-10-13" );

$date2 = date_create( "2003-03-21" );

$diff = date_diff( $date1, $date2 );

echo "Years : ", $diff -> y;

echo "<br>Months : ", $diff -> m;

echo "<br>Days : ", $diff -> d;

?>

</body>

</html>
OUTPUT:
RESULT:

Thus the program to calculate the differences between two date was executed
and verified successfully.
EX NO: 10
DATE:

DISPLAY TIME IN A SPECIFIED ZONE

AIM:
To write a PHP script to display times in a specified time zone.
ALGORITHM:
1. Start the program.

2. Open the HTML tag.

3. Open the body tag,inside the body tag open the PHP script.

4. Using the ini_set() function to find the timezone of given country and its
capital.

5. Print the timezone in a paragraph tag.

6. Close the PHP script and body tag.

7. Close the HTML tag.

8. End the program.


PROGRAM :
<html>

<body>

<?php

ini_set('date.timezone','America/New_York');

echo '<p>'.date("g:i A").'</p>'."\n";

?>

</body>

</html>
OUTPUT:
RESULT:
Thus the program to display specified time in a zone was executed and verified
successfully.

You might also like