Subject: MIS 423- Web Application Development
Instructor: Dr. Walaa Abdelfattah
Question 1
Output |
WELLCOME |
Explanation: |
The reason why "WELCOME" is the output as a result of this code's execution is because it meets the set condition of the if statement. Simply the condition is an arithmetic operation of the values of $x1, $x2, and $x3. Since this operation returns true, the code will execute the statement "echo "WELLCOME" which gives the above output. |
Question 2
Output |
Hello |
Explanation: |
The print statement returns 1, but since the if (!print "Hello") statement uses the! It will return a 0 instead. Therefore only (!print "Hello") will be executed, and the rest will not be executed. |
Question 3
Output |
PHP Parse error: syntax error, unexpected ')' on line 2 |
Explanation: |
The error output is expected since one can easily observe a while loop is used without a condition. Therefore, this will result in an error on the line with the while loop until a set condition is given. |
Question 4
Output |
I'm Do While loop Hello I'm Question 04 |
Explanation: |
First, the output is in different lines since a <br> is used. Further, this code first executes a do…while loop, which executes the "print "I'm Do While loop <br>" statement giving us the first output. Then the second output is printed automatically since "print " Hello I'm Question 04" is outside the do…while loop and does not have a set condition to execute. |
Question 5
Output |
The number is 0 The number is 1 The number is 2 |
Explanation: |
First, we need to understand that this is a do…while loop code. Therefore, this code loops through the block of code given once and then loops again as long as the set condition remains true. In the given code textx is declared and assigned the value 0, which after execution increases the value of $textx by 1. This loop is repeated by executing the statement "echo "The number is " . $testx. "<br>"” until the condition $testx != 3 is met thus giving as the three outputs above. |
Question 6
Output |
I started the loop I finalized the loop |
Explanation: |
This loop code executes only once as the value of i is incremented, thus executing both statements print "I started the loop <br>" and "print "I finalized the loop\n" |
Question 7
Output |
RedGreenYellow |
Explanation: |
The condition "$colors[$x] == "Ball" is true when $colors[$x] is equal to 1 which holds the element “ball”. Therefore, this iteration breaks since the condition is true, and the loop continues until $x is found to be greater than 3. As a result, only ‘RedGreenYellow’ will be printed, and ‘ball’ will be skipped. |
Question 8
Output |
I love arsenalI love manc |
Explanation: |
This PHP case code is supposed to output when the case "arsenal" is found but also executes the statements after "arsenal is found. This is because if a break statement is not found, all the following case blocks execute by default until a break statement is found. |
Question 9
Output |
PHP Parse error: syntax error, unexpected '$x' (T_VARIABLE) on line 9 |
Explanation: |
The "PHP Parse error: syntax error, unexpected '$x' (T_VARIABLE)" on a PHP code is mostly associated with syntax errors like failure to follow the PHP syntax like the use of brackets and semicolons as in the case of this code. |
Question 10
Output |
how are u |
Explanation: |
In this code, $x is declared with no value assigned; therefore, the if statement returns false hence printing what's in the else statement. |
Question 11
Output |
hihello |
Explanation: |
This code is in two parts, where the first part is a do…while loop. Therefore, this code should execute the "print "hi" statement just once since the condition set is while (0). Then the other part is located outside the do…while loop hence automatically executing to give the output of hello. However, these two outputs will appear in one line because there is no <br> statement to separate them. |
Trace the output of the following code:
Question 1
Output:
1000
2000
300
400
5000
Array ( [zero] => 2000 [one] => 3000 [two] => 500 )
Step #1 |
$numbers = array("1000","2000","300","400", "5000"); |
‘numbers’ is declared as an array and assigned 5 values |
|
Step #2 |
foreach ($numbers as $value) |
$value is assigned the array $numbers |
|
Step #3 |
echo "$value <br>"; |
$value is printed and moves to the next line |
|
Step #4 |
$numbers = array("zero" => 2000, "one" => 1000, "two" => 500, "one" => 2000, "one" => 3000) |
Declaration of $numbers as an associative array |
|
Step #5 |
print_r($numbers) |
The contents of variable $numbers are printed |
Question 2
Output: Error
Step #1 |
for ($count = 0; $count<3;$count++); |
Count is set to 0, and it is checked if it's below 3. |
|
Step #2 |
print "hi";break;print "hello"; |
“hi” and “hello” is to be printed, but an error occurs since “break” is used here, and PHP does not allow the use of ‘break’ in this code. The loop should iterate until when count is above 3 or else terminate. However, in this case, the code cannot execute because of the case “statement.” |
Question 3
Output: PHP Parse error: syntax error, unexpected 'else' (T_ELSE) on line 10
Step 1 |
$myarray = array("Cycle" => 2, "Bike" => 5, "Car" => 9); |
Associative array $myarray is declared and assigned values and its keys |
|
Step 2
|
$arrsize = count($myarray); |
The number of elements of the array $myarray is obtained and assigned to the variable $arrsize |
|
Step 3
|
$keys = array_keys($myarray); |
returns the keys from the array $myarray and assigned to the variable $keys |
|
Step 4
|
Krsort($myarray); |
$myarray is sorted here using Ksort statement to sort it in in a descending order in reference to the keys |
|
Step 5
|
for ($x = 0; $x < $arrsize; $x++) |
$x is set to 0, and it is checked if it's below $arrsize |
|
Step 6
|
if($myarray[$keys[$x]] > 2) |
If statements checks whether $myarray[$keys[$x]] is above 2 |
|
Step 7
|
echo "Type is: ".$keys[$x].", "."NUM is: ".$myarray[$keys[$x]]; |
If it returns true, the key and array element output are printed. |
|
Step 8
|
echo "<br>"; |
This command instructs the code to move to the next line of the output screen, but an error occurs because this statement should be in between brackets |
|
Step 9
|
else |
If the condition had returned a false, the else statement is to be executed |
|
Step 10
|
echo "Type is: ".$keys[$x].", "."NUM is: ".$myarray[$keys[$x]]; |
A print statement is executed if the condition is false, which also outputs of the key and array element are printed. Also, an error occurs here because this statement should be in between brackets |