Please answer and show your work. Thumbs up guaranteed. Please add comments so I
ID: 3705263 • Letter: P
Question
Please answer and show your work. Thumbs up guaranteed.
Please add comments so I can understand how you wrote the code. Please keep the code fairly simple as well for these are introductory questions with the basics of PHP taught only.
Thank you!
The following set of short questions will focus on getting you familiar with how to write PHP functions as well as how to make use of the pre-existing PHP functions. All your PHP functions must be declared in the document body section and each functions name must be as specified below. To demonstrate the functionality of each method, you must make function calls in the document body. Include a heading (hl.. h6) that indicates which function is being tested before each function demonstration. The use of Global Variables is forbidden! Test all your functions with some relevant input parameter values.Explanation / Answer
<!DOCTYPE html>
<html>
<body>
<?php
$ArrayDemo = array("Jack"=>"55", "Anita"=>"30", "Ramesh"=>"40", "Sophia"=>"21", "Nastran"=>"41", "William"=>"39", "David"=>"5");
function Sort() {
asort($ArrayDemo ); // to sort the array in ascending order based on the value
foreach($ArrayDemo as $x => $x_value) //to display the printed value
{
echo "Key=" . $x . ", Value=" . $x_value;
echo "<br>";
}
}
Sort(); //function call
?>
</body>
</html>
The php code above allows us to declare an array with key and value pair. These are called Associative Arrays. Here, I have declared array with name ArrayDemo. The above program has the command asort and its argument is the array to be sorted
asort($ArrayDemo )- commands allow us to sort the array in ascending order based on the value
b. To sort based on the key value simply use the command ksort($ArrayDemo )
php code:
$ArrayDemo = array("Jack"=>"55", "Anita"=>"30", "Ramesh"=>"40", "Sophia"=>"21", "Nastran"=>"41", "William"=>"39", "David"=>"5");
ksort($ArrayDemo ); // to sort the array in ascending order based on the key
c. To sort based on the value in descending order use the command arsort($ArrayDemo )
arsort($ArrayDemo );
$ArrayDemo = array("Jack"=>"55", "Anita"=>"30", "Ramesh"=>"40", "Sophia"=>"21", "Nastran"=>"41", "William"=>"39", "David"=>"5");
arsort($ArrayDemo ); // to sort the array in in descending order based on the value
d. . To sort based on the key in descending order use the command krsort($ArrayDemo )
krsort($age);
$ArrayDemo = array("Jack"=>"55", "Anita"=>"30", "Ramesh"=>"40", "Sophia"=>"21", "Nastran"=>"41", "William"=>"39", "David"=>"5");
ksort($ArrayDemo ); // to sort the array in ascending order based on the key
The foreach loop we have used is for printing the data stored key and value part of the array. It iterates and displays all the values
Function average temperature:
<!DOCTYPE html>
<html>
<body>
<?php
function averageTemp()
{
$temperature= array(78,60,62,68,71,-17,52,68,73,85,66,64, 76,63,75,76,73,68,62,73,-10,72,65,80,74,62,62,65,64,0,68,73,75,79,73,77);
$average = array_sum($temperature)/count($temp);
echo "Average is: "$average;
}
averageTemp() //call the function
?>
</body>
</html>
To get the Highest Temperature, what we can do is to simply sort the array in ascending order, using sort() function and take the bottom 4 values
To get Lowest Temperature, sort the array in ascending order, take the top 3 elements:
Find the size of array and decrement till you get 3 values
Code
<!DOCTYPE html>
<html>
<body>
<?php
function averageTemp() {
$temp= array(78,60,62,68,71,-17,52,68,73,85,66,64, 76,63,75,76,73,68,62,73,-10,72,65,80,74,62,62,65,64,0,68,73,75,79,73,77);
$average = array_sum($temp)/count($temp);
echo "Average Temperature is" $average;
echo "<br>";
echo "Highest Temperature is:";
echo "<br>";
$arrlength = count($temp);
sort($temp);
for($x = $arrlength; $x > $arrlength-5 ; $x--) {
echo $temp[$x];
echo "<br>";
}
sort($temp);
echo "Lowest Temperature is:";
for($x = 0; $x < 4; $x++) {
echo $temp[$x];
echo "<br>";
}
}
averageTemp();
?>
</body>
</html>
To search for the word,
For checking if a word exist in a string, find the position of word in string
If it is equal to 1, string is present in beginning
if its equal to length of the string, it is present in end
<!DOCTYPE html>
<html>
<body>
<?php
$a = 'I love PHP';
$len=str_word_count($a);
$beg=1;
$pos = strrpos($a, "I");
echo $pos;
echo $len;
if ($pos=$beg) { //for beginning
echo 'TRUE';
}
if ( $pos=$len) { echo 'TRUE'; // for end of sentence
}
echo "<br>";
?>
</body>
</html>
I hope my code and explanation is clear for you.
I have personally checked working of code
Hope you will get clear insight of the code. Thank you