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

PHP Programs 1 To 5

Uploaded by

mageshkumar6936
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views6 pages

PHP Programs 1 To 5

Uploaded by

mageshkumar6936
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

M22UCSP501 - PRACTICAL V OPEN SOURCE TECHNOLOGY USING PHP

1. simple html form and accept the user name and display the name through php echo
statement program

<!DOCTYPE html>

<html>

<head>

<title></title>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

</head>

<body>

<!-- HTML form with POST method -->

<form method='POST'>

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

<!-- Text input field for the name -->

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

<!-- Submit button to submit the form -->

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

</form>

<?php

// Check if the form is submitted and 'name' is set in $_POST

if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['name'])) {

// Retrieve name from the form and store it in a local variable

$name = $_POST['name'];

// Display a greeting message with the entered name

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

?>

</body>

</html>
2. Write a php script to redirect a user to a different page program.

<?php

// Use the header function to send a raw HTTP header

// In this case, the header function is used to perform a redirection

// The 'Location' header specifies the URL to which the user will be redirected

header('Location: https://www.w3resource.com/');

?>
3. Write a php function to test wheather a number is greater than 30, 20 or 10 using ternary
operator.

<?php

// Function to test a given number using ternary operators

function trinary_Test($n){

// Ternary operators used to check the value of $n and assign a corresponding message to $r

$r = $n > 30

? "greater than 30"

: ($n > 20

? "greater than 20"

: ($n > 10

? "greater than 10"

: "Input a number at least greater than 10!"));

// Display the result with the input number

echo $n . " : " . $r . "\n" . “<br>”;

// Test the function with different input values

trinary_Test(32);

trinary_Test(21);

trinary_Test(12);

trinary_Test(4);

?>
4. Create a php script which display the capital and country name from the given array. Sort
the list by the name of the country.

<?php
// Define an associative array with countries as keys and capitals as values
$ceu = array(
"Italy" => "Rome", "Luxembourg" => "Luxembourg",
"Belgium" => "Brussels", "Denmark" => "Copenhagen",
"Finland" => "Helsinki", "France" => "Paris",
"Slovakia" => "Bratislava", "Slovenia" => "Ljubljana",
"Germany" => "Berlin", "Greece" => "Athens",
"Ireland" => "Dublin", "Netherlands" => "Amsterdam",
"Portugal" => "Lisbon", "Spain" => "Madrid",
"Sweden" => "Stockholm", "United Kingdom" => "London",
"Cyprus" => "Nicosia", "Lithuania" => "Vilnius",
"Czech Republic" => "Prague", "Estonia" => "Tallin",
"Hungary" => "Budapest", "Latvia" => "Riga",
"Malta" => "Valetta", "Austria" => "Vienna",
"Poland" => "Warsaw"
);

// Sort the associative array by values (capitals) in ascending order


asort($ceu);
// Iterate through the sorted array and echo the country and its capital
foreach ($ceu as $country => $capital) {
echo "The capital of $country is $capital" . "\n";
}

?>
5. Write a php script to calculate and display average temperature, five lowest and highest
temperatures.

<?php
// Define a string of monthly temperatures separated by commas
$month_temp = "78, 60, 62, 68, 71, 68, 73, 85, 66, 64, 76, 63, 81, 76, 73,
68, 72, 73, 75, 65, 74, 63, 67, 65, 64, 68, 73, 75, 79, 73";

// Split the string into an array using commas as the delimiter


$temp_array = explode(',', $month_temp);

// Initialize variables for total temperature and the length of the temperature array
$tot_temp = 0;
$temp_array_length = count($temp_array);

// Iterate through the temperature array, calculate the total temperature


foreach($temp_array as $temp)
{
$tot_temp += $temp;
}

// Calculate the average high temperature


$avg_high_temp = $tot_temp / $temp_array_length;

// Display the average temperature


echo "Average Temperature is : " . $avg_high_temp . "" . "<br>";

// Sort the temperature array in ascending order


sort($temp_array);

// Display the list of five lowest temperatures


echo " List of five lowest temperatures :";
for ($i = 0; $i < 5; $i++)
{
echo $temp_array[$i] . ", " . "<br>";
}

// Display the list of five highest temperatures


echo "List of five highest temperatures :";
for ($i = ($temp_array_length - 5); $i < $temp_array_length; $i++)
{
echo $temp_array[$i] . ", " . "<br>";
}
?>

You might also like