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

Example Programs PHP

The document contains multiple PHP scripts demonstrating various functionalities such as displaying country capitals sorted by their names, deleting an element from an array while normalizing keys, calculating average temperatures, and decoding JSON strings into PHP associative arrays. It also explains the use of functions like var_dump, array_walk_recursive, and explode. Each section provides code examples to illustrate the concepts discussed.
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)
6 views6 pages

Example Programs PHP

The document contains multiple PHP scripts demonstrating various functionalities such as displaying country capitals sorted by their names, deleting an element from an array while normalizing keys, calculating average temperatures, and decoding JSON strings into PHP associative arrays. It also explains the use of functions like var_dump, array_walk_recursive, and explode. Each section provides code examples to illustrate the concepts discussed.
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

Q1 $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.

Sample Output :
The capital of Netherlands is Amsterdam
The capital of Greece is Athens
The capital of Germany is Berlin
<?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) ;
foreach($ceu as $country => $capital)
{
echo "The capital of $country is $capital"."\n" ;
}
?>

--------------------------------------------------------------------------------------------------------

Q2. $x = array(1, 2, 3, 4, 5);

Delete an element from the above PHP array. After deleting the element,
integer keys must be normalized.
<?php
$x = array(1, 2, 3, 4, 5);
var_dump($x);
unset($x[3]);
$x = array_values($x);
echo '
';
var_dump($x);
?>
--------------------------------------------------------------
----

Q3. 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
<?php

$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";

$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 " List of five lowest temperatures :";

for ($i=0; $i< 5; $i++)


{

echo $temp_array[$i].", ";

echo "List of five highest temperatures :";

for ($i=($temp_array_length-5); $i< ($temp_array_length); $i+


+)

echo $temp_array[$i].", ";

?>

--------------------------------------------------------------------------------------------------------
JSON String: JavaScript Object Notation (JSON) is a standard text-based
format for representing structured data based on JavaScript object syntax. It is
commonly used for transmitting data in web applications (e.g., sending some
data from the server to the client, so it can be displayed on a web page, or
vice versa). JSON is purely a string with a specified data format — it contains
only properties, no methods. JSON requires double quotes to be used around
strings and property names. Single quotes are not valid other than
surrounding the entire JSON string.

Write a PHP script which decodes the following JSON string.


Sample JSON code :
{"Title": "The Cuckoos Calling",
"Author": "Robert Galbraith",
"Detail": {
"Publisher": "Little Brown"
}}
<?php
function Js_function($value,$key)
{
echo "$key : $value"."\n";
}
$a = '{"Title": "The Cuckoos Calling",
"Author": "Robert Galbraith",
"Detail":
{
"Publisher": "Little Brown"
}
}';
$j1 = json_decode($a,true);
array_walk_recursive($j1,"w3rfunction");
?>

Explanation:
The json_decode() function is used to decode or convert a JSON object to a
PHP object.
Returns the value encoded in JSON in appropriate PHP type. If the JSON object
cannot be decoded it returns NULL
Example:
How to access the values from the PHP object:

<?php
$jsonobj = '{"Peter":35,"Ben":37,"Joe":43}';

$obj = json_decode($jsonobj);

echo $obj->Peter;
echo $obj->Ben;
echo $obj->Joe;
?>

-------------------------------------------------------------------------------------

Store JSON data in a PHP variable, and then decode it into a PHP associative
array:

<?php
$jsonobj = '{"Peter":35,"Ben":37,"Joe":43}';

var_dump(json_decode($jsonobj, true));
?>
------------------------------------------------------------------------------------
The var_dump() function dumps information about one or more variables. The
information holds type and value of the variable(s).
Example:
<html>

<body>

<?php

$a = 32;
echo var_dump($a) . "<br>";

$b = "Hello world!";

echo var_dump($b) . "<br>";

$c = 32.5;

echo var_dump($c) . "<br>";

$d = array("red", "green", "blue");

echo var_dump($d) . "<br>";

$e = array(32, "Hello world!", 32.5, array("red", "green", "blue"));

echo var_dump($e) . "<br>";

// Dump two variables

echo var_dump($a, $b) . "<br>";

?>

</body>

</html>

--------------------------------------------------------------------------------------------------------------------------------------
The array_walk_recursive() function runs each array element in a user-defined
function. The array's keys and values are parameters in the function. The
difference between this function and the array_walk() function is that with
this function you can work with deeper arrays (an array inside an array).
Example:
<?php
function myfunction($value,$key)
{
echo "The key $key has the value $value<br>";
}
$a1=array("a"=>"red","b"=>"green");
$a2=array($a1,"1"=>"blue","2"=>"yellow");
array_walk_recursive($a2,"myfunction");
?>

The array_values() function returns an array containing all the values of an


array.
Example:
<?php
$a=array("Name"=>"Peter","Age"=>"41","Country"=>"USA");
print_r(array_values($a));
?>
The explode() function breaks a string into an array. Note: The "separator" parameter cannot be an
empty string

You might also like