Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

1.

$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") ;Create a PHP script which displays the capital and country name from
the above array $ceu. Sort the list by the capital of the country.
(Country name = capital name )

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<?php
$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") ;
asort($ceu);
echo "<b>Country name = Capital name</b>" ."<br>";
foreach ($ceu as $key => $value) {
echo $key. " = ". $value." <br>";
}
?>
</body>
</html>

Output:

Country name=Capital name


Netherlands = Amsterdam
Greece = Athens
Germany = Berlin
Slovakia = Bratislava
Belgium = Brussels
Hungary = Budapest
Denmark = Copenhagen
Ireland = Dublin
Finland = Helsinki
Portugal = Lisbon
Slovenia = Ljubljana
United Kingdom = London
Luxembourg = Luxembourg
Spain = Madrid
Cyprus = Nicosia
France = Paris
Czech Republic = Prague
Latvia = Riga
Italy = Rome
Sweden = Stockholm
Estonia = Tallin
Malta = Valetta
Austria = Vienna
Lithuania = Vilnius
Poland = Warsaw

2. Write a PHP script to sort the following associative array.


array("Sophia"=>"31","Jacob"=>"41","William"=>"39","Ramesh"=>"40") in
a) ascending order sort by value
b) ascending order sort by Key
c) descending order sorting by Value
d) descending order sorting by Key

<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<?php
$arr= array("Sophia"=>"31","Jacob"=>"41","William"=>"39","Ramesh"=>"40");
//a. ascending order sort by value
asort($arr);
echo "<b>The given value is sorted in ascending order.</b>"."<br>";
foreach($arr as $key => $value) {
echo "key=" . $key . ", value=" . $value;
echo "<br>";
}
//b. ascending order sort by Key
ksort($arr);
echo "<b>The given key is sorted in ascending order.</b>"."<br>";
foreach ($arr as $key => $value) {
echo "key=" . $key . ", value=" . $value;
echo "<br>";
}
//c. descending order sorting by Value
arsort($arr);
echo "<b>The given value is sorted in descending order.</b>"."<br>";
foreach($arr as $key => $value) {
echo "key=" . $key . ", value=" . $value;
echo "<br>";
}
//d. descending order sorting by Value
krsort($arr);
echo "<b>The given key is sorted in descending order.</b>"."<br>";
foreach($arr as $key => $value) {
echo "Key=" . $key . ", value=" . $value;
echo "<br>";
}
?>
</body>
</html>

Output:

The given value is sorted in ascending order.


key=Sophia, value=31
key=William, value=39
key=Ramesh, value=40
key=Jacob, value=41
The given key is sorted in ascending order.
key=Jacob, value=41
key=Ramesh, value=40
key=Sophia, value=31
key=William, value=39
The given value is sorted in descending order.
key=Jacob, value=41
key=Ramesh, value=40
key=William, value=39
key=Sophia, value=31
The given key is sorted in descending order.
Key=William, value=39
Key=Sophia, value=31
Key=Ramesh, value=40
Key=Jacob, value=41

3. Write a PHP script to calculate and display average temperature, five lowest and highest
temperatures. Recorded temperatures: 78, 60, 62, 68, 71, 68, 73, 85, 66, 64, 76, 63, 75, 76, 73, 68, 62,
73, 72, 65, 74, 62, 62, 65, 64, 68, 73, 75, 79, 73

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<?php
$arr = array(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);
$temp =0;
$length = count($arr);
for($i= 0; $i < $length ; $i++)
{
$temp += $arr[$i];
}
$avg = $temp/$length;
echo "Average Temp is : " .$avg;
echo "<br>";
sort($arr);
echo " List of five lowest temperatures :";
for ($i=0; $i< 5; $i++)
{
echo $arr[$i].", ";
}
echo "<br>";
rsort($arr);
echo " List of five highest temperatures :";
for ($i=0; $i< 5; $i++)
{
echo $arr[$i].", ";
}
?>
</body>
</html>
Output:
Average Temp is : 70.6
List of five lowest temperatures :60, 62, 63, 63, 64,
List of five highest temperatures :85, 81, 79, 78, 76,

4. Write a PHP function that checks whether a passed string is a palindrome or not?

<!DOCTYPE html>
<html >
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<?php
function Palindrome($string){
if (strrev($string) == $string){
return 1;
}
else{
return 0;
}
}
$name = "DAD";
if(Palindrome($name)){
echo $name ." "."is a Palindrome";
}
else {
echo $name ." "."Not a Palindrome";
}
?>
</body>
</html>

Output:
DAD is a Palindrome

5. Write a PHP script to split the following string. Sample string : '082307', Expected Output : 08:23:07

<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<?php
$str = "082307";
$split = substr(chunk_split($str,2,":"),0,-1); //splits string into smaller series.
echo $split;
?>
</body>
</html>

Output:
08:23:07

6. Write a PHP script which will display the copyright information in the following format. To get current
year you can use the date() function. Expected Format : © 2013 PHP Exercises - w3resource
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<title></title>
</head>
<body>
<?php
echo "&copy"." " .date('Y')." "."PHP Exercises - w3resource";
?>
</body>
</html>

Output:
© 2021 PHP Exercises - w3resource

Conclusion:
Here, ‘&copy’ is used to display the copyright symbol and date function is used to get the present date .
7. Write a PHP script to calculate the current age of a person.
Sample date of birth : 11.4.1987

Output : Your age : 27 years, 1 month, 29 days

<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<?php
$dob = "11.4.1987";
$bday = new DateTime($dob);
$today = new DateTime();
$difference = $today->diff($bday);
$age = $difference->y;
echo "Your age : ".$age. "years";
?>
</body>
</html>

Output:

Your age: 33years

Conclusion:

Age is calculated using date of birth and DateTime() function.

You might also like