Bird
0
0

What will be the output of this PHP code?

medium📝 Predict Output Q5 of 15
PHP - Array Functions
What will be the output of this PHP code?
$arr = ["x", "y"];
$padded = array_pad($arr, 4, "z");
print_r($padded);
AArray ( [0] => x [1] => y [2] => z [3] => z )
BArray ( [0] => z [1] => z [2] => x [3] => y )
CArray ( [0] => x [1] => y )
DError: Missing size parameter
Step-by-Step Solution
Solution:
  1. Step 1: Understand array_pad

    It pads the array to the specified size by adding the pad value at the end if size is positive.
  2. Step 2: Analyze code

    Original array has 2 elements, padded to size 4 with "z" at the end.
  3. Final Answer:

    Array ( [0] => x [1] => y [2] => z [3] => z ) -> Option A
  4. Quick Check:

    Positive size pads at end [OK]
Quick Trick: Positive size pads at end, negative at start [OK]
Common Mistakes:
  • Confusing padding at start vs end
  • Expecting no change if array smaller than size

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes