In: Computer Science
Dear Student ,
As per the requirement submitted above , kindly find the below solution.
Question 1:
Here a new php file with name "SampleScript.php" is created, which contains following code.
SampleScript.php :
<?php
//declaring variable to store sum
$sum=0;
//function to add number
function addNumbers($num)
{
global $sum;/* reference to global scope variable */
$sum=$sum+$num;
}
//calling function to add numbers
addNumbers(10);
addNumbers(20);
addNumbers(30);
//displaying total
echo "Total :".$sum."<br/>";
?>
======================================================
Output : Open web page in the browser and will get the screen as shown below
Screen 1 :
**********************************
Question 2 :
<?php
echo "<h1><i>This is from PHP</i></h1>";
?>
Output :When web page opened in the browser will get the screen as shown below.
************************************
Question 3 :
Answer :LocalHost
Explanation :Constant is not needed to access with $.
<?php
define ("LocalHost","127.0.0.1");
echo LocalHost;
?>
Output :After opening this in the browser will get the screen as shown below
***************************************
Question 4 :
Answer :Given code will print infinite time I love PHP programming! because there is no increment condition for i.Every time i will become true .Hence the output.
Code :
<?php
$count = 0;
while($count<10)
echo "I love PHP programming!";
?>
Output :
*******************************
Question 5 :
Answer: True
Explanation :isset() will return true if parameter exists otherwise false.
********************************************
Question 6 :
Answer :True
Explanation :Data submitted through the HTTP post method is store in array $_POST
***************************************
Question 7 :
Code :
<?php
//declaring array of smartphones
$smartPhones= array('iphone' => 699,
'blackberry'=>300,
'Samsung'=>799,
'Nokia'=>120);
//using for each to display key and value
foreach($smartPhones as $name => $price)
{
//display name and price
echo $name." ".$price."<br/>";
}
?>
Output :
***********************************
NOTE : PLEASE FEEL FREE TO PROVIDE FEEDBACK ABOUT THE SOLUTION.