[HELP]PHP Challenge Explanation Needed | Sololearn: Learn to code for FREE!


[HELP]PHP Challenge Explanation Needed

Hey guys please I will appreciate if I can be explained how the PHP challenge answers are gotten. I'll add more questions. Thanks!!! N0. 1 What will be the output of the code snippet? $x = array("aaa", "", "ccc", "ddd", ""); $y = array_unique($x); echo count($x).",".count($y); ANS: 5,4 N0.2 What is the output of this code? for($k = 0; $k < 8; $k++) if($k % 2!= 0){ if($k % 5 == 0) echo $k - 2; else echo $k; } ANS: 1337 N0.3 What is the output of this code? $arr = array("x" => array(1,2,3), "y" => 5); $y = 0; foreach($arr as $x) if(is_array($x)) foreach($x as $value) $y += $value; else $y *= $x; echo $y; ANS: 30 N0.4 What is the output of this code? $cats = array("Tom", "Joe", "Glenn", "Martie", "Jeff"); $queue = count($cats) + 4; echo $queue; $queue *= 1111; echo $queue; ANS:99999 N0.5 What is the output of this code? $i = 0; $num = 80; while($i < 20){ $num -= 2; $i += 0.5; } echo("$num"); ANS:0

12/25/2018 9:31:18 PM

Ikechukwu Okonkwo

6 Answers

New Answer


No. 1 Array consists of 5 elements,so count $x is 5. 4 of them are unique (you have an empty string 2 times) so count($y) is 4. No.2 This loop cares only about odd numbers, even numbers won't output anything. If k is 1, else statement is echoing 1. If k is 3, again the condition in nested if statement is not satisfied, so, k is echoed in else statement - we have 13 so far. If k is 5, the condition in nested if statement is satisfied and 5-2 is printed. We have 133 so far. If k is 7, it's the same as in the case of 1 and 3 so 7 is printed in else statement. The result is 1337.


dρlυѕρlυѕ, Vasiliy, TheWhiteCat💡, Sekiro thanks to you guys...I'll carefully go through each of your answer..I appreciate you guys helping me out..I'll post more questions that I find confusing...👍👍👍


No.3 Here you have associative array with nested array for key "x". Two foreach loops => the first running through the keys the second one running through the values. Also there is conditional check if on a certain key the value is array then add the value to "y", if not multiply the value to "y". So the result is (1 + 2 + 3)* 5 = 30. No.4 Here you count the elements in the array => 5 and add 4, variable queue is now 9 => and print 9. The value of queue is changed again and printed => 9 * 1111 = 9999. So the result is 99999.


N0.5 "$i+=0.5" increases the cycle 2 times => ($num-=2)*40 => 80/40=2 As the "while" loop first runs, then $num=2-2


N 0.4 coun($cats)=5 because the $cats array consists of 5 elements => $queue=5+4 => $queue=9*1111


No.3 You have a multidimensional array with another array inside and one single int value. Now the foreach statement goes through the array and then there is the condition if the value inside the first array is another array then will add all the values of the array to $y, in this case the index “x” of $arr is another array so it will be $y = 1+2+3 -> $y = 6, then when the foreach loop arrive to the index y of the $arr it will check if it is an array, but it is not, so instead of adding it to $y it will multiply it so it will be $y = $y * 5 -> $y= 6 * 5 -> $y = 30