Answer:
The answer to this question can be given as:
code 1:
<?php
//define 1-D array
$fruits = array('Apple', 'IBM', 'Zerox', 'Stratus', 'Aspire');
for($k = 0; $k < count($fruits); $k++) //use for loop.
{
echo $fruits[$k]." "; //print values.
}
?>
Code 2:
<?php
//define 1-D array
$fruits = array('Apple', 'IBM', 'Zerox', 'Stratus', 'Aspire');
foreach($fruits as $element) //use foreach loop
{
echo $element." ";
//print values.
}
?>
code 3:
Answer:In this code default arguments are $argB ,$argC because they are define if we don't pass value to these argument so it will take default value.
<?php
function foo($argA, $argB = 5, $argC = 10) //define function
{
echo"argA: ".$argA;
echo " ";
echo"argB: ".$argB;
echo " ";
echo"argC: ".$argC;
}
foo("abc");
//calling function
?>
code 4:
Answer: The variable $arg1 and $arg2 are optional arguments. If we want to pass the value to them so they take that value otherwise they use there default value.
<?php
function foo($argx, $arg1 = '', $arg2 ='') //define function.
{
echo"argx: ".$argx; //print value
echo " ";
echo"arg1: ".$arg1;
echo " ";
echo"arg2: ".$arg2;
}
foo("abc"); //calling function
?>
Explanation:
In this question there are 4 code that can be explain as:
In all the code all the working done in the php script that is
<?php
//codes
?>
In code 1 we define 1-D array.In this array we insert elements and use the for loop for print all elements. In php echo is used for print elements.
In code 2 we define the 1-D array in the array we pass the value and use the foreach loop for print all the value. In the PHP foreach loop is a special loop, it is used for print key and value.
In code 3 we define the function that is function foo().In function we pass three variable that is $argA, $argB and $argC. In variable $arg we don't pass any value. but in the other two variables, we pass the value that shows both 2 variables are default arguments because they are defined. If we don't pass the value to these arguments so it will take default value. Then we call the function and print its value.
In code 4 we define the function that is function foo().In function we pass three variable that is $argx, $arg1 and $arg2. In this variable, we don't pass any value. We only assign the value in the first variable because
The variable $arg1 and $arg2 are optional arguments. If we want to pass the value to them so they take that value otherwise they use their default value. Then we call the function and print its value.