PHP Programs 1 To 5
PHP Programs 1 To 5
1. simple html form and accept the user name and display the name through php echo
statement program
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<form method='POST'>
</form>
<?php
$name = $_POST['name'];
?>
</body>
</html>
2. Write a php script to redirect a user to a different page program.
<?php
// 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 trinary_Test($n){
// Ternary operators used to check the value of $n and assign a corresponding message to $r
$r = $n > 30
: ($n > 20
: ($n > 10
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"
);
?>
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";
// Initialize variables for total temperature and the length of the temperature array
$tot_temp = 0;
$temp_array_length = count($temp_array);