PHP - ArraysYou want to create an indexed array of the first 5 positive integers using a loop. Which code correctly does this?A$arr = array(); for ($i = 0; $i < 5; $i++) { $arr[$i] = $i; }B$arr = []; for ($i = 1; $i <= 5; $i++) { $arr[] = $i; }C$arr = []; for ($i = 1; $i < 5; $i++) { $arr[$i] = $i; }D$arr = array(); for ($i = 1; $i <= 5; $i++) { $arr[$i] = $i; }Check Answer
Step-by-Step SolutionSolution:Step 1: Understand loop and array appendingUsing $arr[] = $i; appends $i to the end of the array with automatic indexing starting at 0.Step 2: Check loop range and array fillingThe loop runs from 1 to 5 inclusive, adding values 1 through 5 to the array.Final Answer:$arr = []; for ($i = 1; $i <= 5; $i++) { $arr[] = $i; } -> Option BQuick Check:Appending with [] in loop = $arr = []; for ($i = 1; $i <= 5; $i++) { $arr[] = $i; } [OK]Quick Trick: Use $arr[] = value to append in loops [OK]Common Mistakes:Using wrong loop boundsAssigning to $arr[$i] starting at 1 causing gapsNot appending properly
Master "Arrays" in PHP9 interactive learning modes - each teaches the same concept differentlyLearnWhyDeepVisualTryChallengeProjectRecallTime
More PHP Quizzes Conditional Statements - If statement execution flow - Quiz 12easy Functions - Why functions are needed - Quiz 11easy Loops - Foreach loop for arrays - Quiz 7medium Operators - Arithmetic operators - Quiz 1easy Operators - Logical operators - Quiz 13medium Output and String Handling - Escape sequences in strings - Quiz 10hard Output and String Handling - String interpolation in double quotes - Quiz 2easy Output and String Handling - Heredoc and nowdoc syntax - Quiz 6medium PHP Basics and Execution Model - PHP Installation and Setup - Quiz 4medium PHP Request Lifecycle - Comparison with long-running servers (Node.js) - Quiz 6medium